Part Number Hot Search : 
AP3102L MBT8050 TPSMB39 BAT54A MMBT5551 M200V8X1 2SK33 91001
Product Description
Full Text Search
 

To Download ATMEGA16107 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  1 features ? high-performance, low-power avr ? 8-bit microcontroller  advanced risc architecture ? 130 powerful instructions ? most single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation ? up to 8 mips throughput at 8 mhz ? on-chip 2-cycle multiplier  program and data memories ? 16k bytes of non-volatile in-system programmable flash endurance: 1,000 write/erase cycles ? optional boot code memory with independent lock bits self-programming of program and data memories ? 512 bytes of non-volatile in-system pr ogrammable eeprom endurance: 100,000 write/erase cycles ? 1k byte of internal sram ? programming lock for software security  peripheral features ? two 8-bit timer/counters with separate prescaler and pwm ? expanded 16-bit timer/counter system with separate prescaler, compare, capture modes and dual 8-, 9-, or 10-bit pwm ? dual programmable serial uarts ? master/slave spi serial interface ? real-time counte r with separate oscillator ? programmable watchdog timer with separate on-chip oscillator ? on-chip analog comparator  special microcontroller features ? power-on reset ? external and internal interrupt sources ? three sleep modes: idle, power-save and power-down  power comsumption at 4 mhz, 3.0v, 25 c ? active 3.0 ma ? idle mode 1.2 ma ? power-down mode < 1 a  i/o and packages ? 35 programmable i/o lines ? 40-lead pdip and 44-lead tqfp  operating voltages ? 2.7v - 5.5v for the atmega161l ? 4.0v - 5.5v for the atmega161  speed grades ? 0 - 4 mhz for the atmega161l ? 0 - 8 mhz for the atmega161  commercial and industri al temperature ranges disclaimer typical values contained in this data sheet are based on simulations and characteriza- tion of other avr microcontrollers manufactured on the same process technology. min and max values will be available after the device is characterized. 8-bit microcontroller with 16k bytes of in-system programmable flash atmega161 atmega161l note: not recommended in new designs. rev. 1228d?avr?02/07
2 atmega161(l) 1228d?avr?02/07 pin configuration 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 (oc0/t0) pb0 (oc2/t1) pb1 (rxd1/ain0) pb2 (txd1/ain1) pb3 (ss) pb4 (mosi) pb5 (miso) pb6 (sck) pb7 reset (rxd0) pd0 (txd0) pd1 (int0) pd2 (int1) pd3 (tosc1) pd4 (oc1a/tosc2) pd5 (wr) pd6 (rd) pd7 xtal2 xtal1 gnd vcc pa0 (ad0) pa1 (ad1) pa2 (ad2) pa3 (ad3) pa4 (ad4) pa5 (ad5) pa6 (ad6) pa7 (ad7) pe0 (icp/int2) pe1 (ale) pe2 (oc1b) pc7 (a15) pc6 (a14) pc5 (a13) pc4 (a12) pc3 (a11) pc2 (a10) pc1 (a9) pc0 (a8) 1 2 3 4 5 6 7 8 9 10 11 33 32 31 30 29 28 27 26 25 24 23 (mosi) pb5 (miso) pb6 (sck) pb7 reset (rxd0) pd0 nc* (txd0) pd1 (int0) pd2 (int1) pd3 (tosc1) pd4 (ocia/tosc2) pd5 pa4 (ad4) pa5 (ad5) pa6 (ad6) pa7 (ad7) pe0 (icp/int2) nc* pe1 (ale) pe2 (oc1b) pc7 (a15) pc6 (a14) pc5 (a13) 44 43 42 41 40 39 38 37 36 35 34 12 13 14 15 16 17 18 19 20 21 22 (wr) pd6 (rd) pd7 xtal2 xtal1 gnd nc* (a8) pc0 (a9) pc1 (a10) pc2 (a11) pc3 (a12) pc4 pb4 (ss) pb3 (txd1/ain1) pb2 (rxd1/ain0) pb1 (oc2/t1) pb0 (oc0/t0) nc* vcc pa0 (ad0) pa1 (ad1) pa2 (ad2) pa3 (ad3) * nc = do not connect (can be used in future devices) tqfp pdip
3 atmega161(l) 1228d?avr?02/07 description the atmega161 is a low-power cmos 8-bit microcontroller based on the avr risc architecture. by executing powerful instruct ions in a single clock cycle, the atmega161 achieves throughputs approaching 1 mips per mhz allowing the system designer to optimize power consumption versus processing speed. the avr core combines a rich instruction set with 32 general purpose working registers. all the 32 registers are directly connected to the arithmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. the resulting architec- ture is more code-efficient while achieving throughputs up to ten times faster than conventional cisc microcontrollers. the atmega161 provides the following features: 16k bytes of in-system or self- programmable flash, 512 bytes eeprom, 1k byte of sram, 35 general purpose i/o lines, 32 general purpose working registers, real-time counter, three flexible timer/counters with compare modes, internal and external interrupts, two programma- ble serial uarts, programmable watchdog ti mer with internal osc illator, an spi serial port and three software-selectable power saving modes. the idle mode stops the cpu while allowing the sram, timer/counters, spi port and interrupt system to continue functioning. the power-down mode saves the register and sram contents but freezes the oscillator, disabling all ot her chip functions unt il the next external interrupt or hard- ware reset. in power-save mode, the timer oscillator cont inues to run, allowing the user to maintain a timer base while the rest of the device is sleeping. the device is manufactured using atmel?s high-density non-volatile memory technology. the on-chip flash program memory can be reprogrammed using the self-programming capability through th e boot block and an isp through th e spi port, or by using a conven- tional non-volatile memory programmer. by combining an enhanced risc 8-bit cpu with in-system programmable flash on a monolithic chip, the atmel atmega161 is a powerful microcontroller that provides a hi ghly flexible and cost-effective solution to many embedded control applications. the atmega161 avr is supported with a full suite of program and system development tools including: c compilers, macro assemblers, program debugger/simulators, in-cir- cuit emulators and evaluation kits.
4 atmega161(l) 1228d?avr?02/07 block diagram figure 1. the atmega161 block diagram programming logic spi uarts pb0 - pb7 vcc gnd + - analog comparator 8-bit data bus data dir. reg. porta data register porta porta drivers pa0-pa7 data dir. reg. portc data register portc portc drivers pc0-pc7 portd drivers pd0 - pd7 data dir. reg. portd data register portb portb drivers porte drivers pe0 - pe2 data reg. porte data dir reg. porte program counter internal oscillator watchdog timer stack pointer program flash mcu control register sram general purpose registers instruction register timer/ counters instruction decoder timing and control oscillator interrupt unit eeprom status register z y x alu reset xtal2 xtal1 control lines data dir. reg. portb data register portd
5 atmega161(l) 1228d?avr?02/07 pin descriptions vcc supply voltage. gnd ground. port a (pa7..pa0) port a is an 8-bit bi-directional i/o port. port pins can provide internal pull-up resistors (selected for each bit). the port a output bu ffers can sink 20 ma and can drive led dis- plays directly. when pins pa0 to pa7 are us ed as inputs and are externally pulled low, they will source current if the in ternal pull-up resistors are ac tivated. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. port a serves as a multiplexed address/data port when using external memory interface. port b (pb7..pb0) port b is an 8-bit bi-directional i/o port with internal pull-up resistors. the port b output buffers can sink 20 ma. as i nputs, port b pins that are ex ternally pulled low will source current if the pull-up resistors are activated. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. port b also serves the functions of various special features of the atmega161 as listed on page 92. port c (pc7..pc0) port c is an 8-bit bi-directional i/o port with internal pull-up resistors. the port c output buffers can sink 20 ma . as inputs, port c pins that ar e externally pulled low will source current if the pull-up resistors are activated. the port c pins are tri-stated when a reset condition becomes active, even if the clock is not running. port c also serves as an address high out put when using external memory interface. port d (pd7..pd0) port d is an 8-bit bi-directional i/o port with internal pull-up resistors. the port d output buffers can sink 20 ma . as inputs, port d pins that ar e externally pulled low will source current if the pull-up resistors are activated. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. port d also serves the functions of various special features of the atmega161 as listed on page 101. port e (pe2..pe0) port e is a 3-bit bi-directional i/o port with internal pull-up resistors. the port e output buffers can sink 20 ma. as i nputs, port e pins that are ex ternally pulled low will source current if the pull-up resistors are activated. the port e pins are tri-stated when a reset condition becomes active, even if the clock is not running. port e also serves the functions of various special features of the atmega161 as listed on page 107. reset reset input. a low level on this pin for more than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. xtal1 input to the inverting oscillato r amplifier and input to the in ternal clock operating circuit. xtal2 output from the invert ing oscillator amplifier.
6 atmega161(l) 1228d?avr?02/07 crystal oscillator xtal1 and xtal2 are input and output, respectively, of an inverting amplifier that can be configured for use as an on-chip oscillator, as shown in figure 2. either a quartz crystal or a ceramic resonator may be used. to drive the device from an external clock source, xtal2 should be left unconnected while xtal1 is driven as shown in figure 3. figure 2. oscillator connections figure 3. external clock drive configuration xtal2 xtal1 gnd c2 c1 xtal2 xtal1 gnd nc external oscillator signal
7 atmega161(l) 1228d?avr?02/07 architectural overview the fast-access register file concept contains 32 x 8-bit general purpose working reg- isters with a single clock cycle access time. this means that during one single clock cycle, one arithmetic logic unit (alu) operation is executed. two operands are output from the register file, the operation is executed and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16-bit indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the three address pointers is also used as the address pointer for the constant table look-up func- tion. these added function registers are the 16-bit x-register, y-register and z-register. the alu supports arithmetic and logic functions between registers or between a con- stant and a register. single register operations are also executed in the alu. figure 4 shows the atmega161 avr risc microcontroller architecture. figure 4. the atmega161 avr risc architecture 8k x 16 program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registers alu status and control interrupt unit spi unit 8-bit timer/counter with pwm and rtc watchdog timer analog comparator 32 i/o lines 512 x 8 eeprom data bus 8-bit serial uart0 16-bit timer/counter with pwm 8-bit timer/counter with pwm 1024 x 8 data sram direct addressing indirect addressing serial uart1
8 atmega161(l) 1228d?avr?02/07 in addition to the register operation, the conventional memory addressing modes can be used on the register file. this is enabled by t he fact that the register file is assigned the 32 lowermost data space addresses ($00 - $1f), allowing them to be accessed as though they were ordinary memory locations. the i/o memory space contains 64 addresses for cpu peripheral functions such as control registers, timer/counters, and other i/o functions. the i/o memory can be accessed directly or as the data space locations following those of the register file, $20 - $5f. the avr uses a harvard architecture concept ? with separate memories and buses for program and data. the program memory is executed with a two-stage pipeline. while one instruction is being executed, the next instruction is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is self-programmable flash memory. with the jump and call instructions, the whole 8k word address space is directly accessed. most avr instructions have a single 16-bit word format. every program memory address contains a 16- or 32-bit instruction. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in the general data sram and, consequently, the stack size is only limited by the total sram size and the usage of the sram. all user programs must initialize the sp (stack pointer) in the reset routine (before subroutines or interrupts are executed). the 16-bit stack pointer is read/write accessible in the i/o space. the 1k byte data sram can be easily accessed through the five different addressing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps.
9 atmega161(l) 1228d?avr?02/07 figure 5. memory maps a flexible interrupt module has its control registers in the i/o space with an additional global interrupt enable bit in the status register. all the different interrupts have a sepa- rate interrupt vector in the interrupt vector table at the beginning of the program memory. the different interrupts have priority in accordance with their interrupt vector position. the lower the interrupt vector address, the higher the priority. 32 gen. purpose working registers 64 i/o registers internal sram (1024 x 8) $0000 $001f $005f $0060 $045f $0020 $000 $1fff data memory program memory program flash (8k x 16) $0460 external sram (0 - 63k x 8) $ffff
10 atmega161(l) 1228d?avr?02/07 the general purpose register file figure 6 shows the structure of the 32 general purpose working registers in the cpu. figure 6. avr cpu general purpose working registers all the register operating instructions in the instruction set have direct and single cycle access to all registers. the only exceptions are the five constant arithmetic and logic instructions sbci, subi, cpi, andi, and ori between a constant and a register, and the ldi instruction for load immediate constant data. these instructions apply to the second half of the registers in the register file ? r16..r31. the general sbc, sub, cp, and, and or, and all other operations between two registers or on a single register apply to the entire register file. as shown in figure 6, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user data space. although not being phys- ically implemented as sram locations, this memory organization provides great flexibility in access of the registers, as the x- , y-, and z-registers ca n be set to index any register in the file. the x-register, y-register and z-register the registers r26..r31 have some added f unctions to their general purpose usage. these registers are address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as: figure 7. x-, y-, and z-registers 7 0 addr. r0 $00 r1 $01 r2 $02 ? r13 $0d general r14 $0e purpose r15 $0f working r16 $10 registers r17 $11 ? r26 $1a x-register low byte r27 $1b x-register high byte r28 $1c y-register low byte r29 $1d y-register high byte r30 $1e z-register low byte r31 $1f z-register high byte 15 0 x-register 7 0 7 0 r27 ($1b) r26 ($1a) 15 0 y-register 7 0 7 0 r29 ($1d) r28 ($1c) 15 0 z-register 7 0 7 0 r31 ($1f) r30 ($1e)
11 atmega161(l) 1228d?avr?02/07 in the different addressing modes, these addr ess registers have functions as fixed dis- placement, automatic increment and decrement (see the descriptions for the different instructions). alu ? arithmetic logic unit the high-performance avr alu operates in direct connection with all the 32 general purpose working registers. within a single clock cycle, alu operations between regis- ters in the register file are executed. the alu operations are divided into three main categories ? arithmetic, logical and bit functions. atmega161 also provides a powerful multiplier supporting both signed/unsigned multiplication and fractional format. see the instruction set section fo r a detailed description. self-programmable flash program memory the atmega161 contains 16k bytes of on-chip self-programmable and in-system pro- grammable flash memory for program storage. since all instructions are 16- or 32-bit words, the flash is organized as 8k x 16. the flash memory has an endurance of at least 1,000 write/erase cycles. the atmega 161 program counter (pc) is 13 bits wide, thus addressing the 8,192 program memory locations. see page 110 for a detailed description of flash data downloading. see page 13 for the different program memory addressing modes. eeprom data memory the atmega161 contains 512 bytes of data eeprom memo ry. it is organized as a sep- arate data sp ace in which single bytes can be r ead and written. the eeprom has an endurance of at least 100,000 write/erase cycles per location. the interface between the eeprom and the cpu is descri bed on page 60, specifying the eeprom address reg- isters, the eeprom data register and the eeprom control register. for the spi data downloading, see page 125 for a detailed description.
12 atmega161(l) 1228d?avr?02/07 sram data memory figure 8 shows how the atmega161 sram memory is organized. figure 8. sram organization the lower 1120 data memory locations address the register file, the i/o memory and the internal data sram. the first 96 locations address the register file and i/o memory and the next 1k locations address the internal data sram. an optional external data memory device can be placed in the same sr am memory space. this memory device will occupy the locations following the intern al sram and up to as much as 64k - 1, depending on external memory size. when the addresses accessing the data memory space exceed the internal data sram locations, the memory device is accessed using the same instructions as for the internal data sram access. when the internal data space is accessed, the read and write strobe pins (rd and wr ) are inactive during the whole access cycle. external memory operation is enabled by setting the sre bit in the mcucr register. see ?interface to external memory? on page 84 for details. accessing external memory takes one addit ional clock cycle per byte compared to access of the internal sram. this means that the commands ld, st, lds, sts, push, and pop take one additional clock cycle. if the stack is placed in external mem- ory, interrupts, subrou tine calls and return s take two clock cycles extra because the 2- byte program counter is pushed and popped . when external memory interface is used with wait state, two additional clock cycles are used per byte. this has the following effect: data transfer instructions take two extra clock cycles, whereas interrupt, subrou- tine calls and returns will need four clock cycles more than specified in the instruction set manual. register file data address space r0 $0000 r1 $0001 r2 $0002 ?? r29 $001d r30 $001e r31 $001f i/o registers $00 $0020 $01 $0021 $02 $0022 ?? $3d $005d $3e $005e $3f $005f internal sram $0060 $0061 ? $045e $045f
13 atmega161(l) 1228d?avr?02/07 the five different addressing modes for the data memory cover: direct, indirect with displacement, indirect, indirect with pre-decrement and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode features a 63-address locations reach from the base address given by the y- or z-register. when using register indirect addressing modes with automatic pre-decrement and post-increment, the address registers x, y, and z are decremented and incremented. the 32 general purpose working registers, 64 i/o registers and the 1k byte of internal data sram in the atmega161 are all accessible through all these addressing modes. see the next section for a detailed description of the different addressing modes. program and data addressing modes the atmega161 avr risc microcontroller supports powerful and efficient addressing modes for access to the program memory (flash) and data memory (sram, register file and i/o memory). this section describes the different addressing modes supported by the avr architecture. in the figures, op means the operation code part of the instruc- tion word. to simplify, not all figures show the exact location of the addressing bits. register direct, single register rd figure 9. direct single register addressing the operand is contained in register d (rd). register direct, two registers rd and rr figure 10. direct register addressing, two registers register file 15 op d d 31 0 40 0 d r 31 register file op r d 15 9 5 4 0
14 atmega161(l) 1228d?avr?02/07 operands are contained in registers r (rr) and d (rd). the result is stored in register d (rd). i/o direct figure 11. i/o direct addressing operand address is contained in six bits of the instruction word. n is the destination or source register address. data direct figure 12. direct data addressing a 16-bit data address is contained in the 16 lsbs of a two-word instruction. rd/rr spec- ify the destination or source register. 0 63 0 5 n op 15 p i/o memory op rr/rd 16 31 15 0 16 lsbs $0000 $ffff 20 19 data space
15 atmega161(l) 1228d?avr?02/07 data indirect with displacement figure 13. data indirect with displacement operand address is the result of the y- or z-register contents added to the address con- tained in six bits of the instruction word. data indirect figure 14. data indirect addressing operand address is the contents of the x-, y-, or z-register. data indirect with pre- decrement figure 15. data indirect addressing with pre-decrement the x-, y-, or z-register is decremented before the operation. operand address is the decremented contents of the x-, y-, or z-register. data space $0000 $ffff y or z - register op a n 0 0 5 6 10 15 15 data space $0000 $ffff x, y, or z - register 0 15 data space $0000 $ffff x, y, or z - register 0 15 -1
16 atmega161(l) 1228d?avr?02/07 data indirect with post- increment figure 16. data indirect addressing with post-increment the x-, y-, or z-register is incremented after the operation. operand address is the con- tents of the x-, y-, or z-register prior to incrementing. constant addressing using the lpm instruction figure 17. code memory constant addressing constant byte address is specified by the z-register contents. the 15 msbs select word address (0 - 8k), the lsb selects low byte if cleared (lsb = 0) or high byte if set (lsb = 1). indirect program addressing, ijmp and icall figure 18. indirect program memory addressing data space $0000 $ffff x, y, or z - register 0 15 1 $1fff $000 program memory z-register 15 1 0 $1fff $000 program memory 15 0 z-register
17 atmega161(l) 1228d?avr?02/07 program execution continues at address contained by the z-register (i.e., the pc is loaded with the contents of the z-register). relative program addressing, rjmp and rcall figure 19. relative program memory addressing program execution continues at address pc + k + 1. the relative address k is -2048 to 2047. direct program addressing, jmp and call figure 20. direct program addressing program execution continues at the address immediate in the instruction words. memory access times and instruction execution timing this section describes the general access timing concepts for instruction execution and internal memory access. the avr cpu is driven by the system clock ?, directly generated from the external clock crystal for the chip. no in ternal clock division is used. figure 21 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast-access register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions per clocks and functions per power unit. $1fff $000 program memory 15 pc 15 12 11 op k 0 0 op 16 21 20 31 15 0 16 lsbs program memory $0000 $1fff
18 atmega161(l) 1228d?avr?02/07 figure 21. the parallel instruction fetches and instruction executions figure 22 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed and the result is stored back to the destination register. figure 22. single cycle alu operation the internal data sram access is performed in two system clock cycles as described in figure 23. figure 23. on-chip data sram access cycles system clock ? 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4 system clock ? total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 system clock ? wr rd data data address address t1 t2 t3 t4 prev. address read write
19 atmega161(l) 1228d?avr?02/07 l/o memory the i/o space definition of the atmega161 is shown in table 1. table 1. atmega161 i/o space (1) i/o address (sram address) name function $3f($5f) sreg status register $3e ($5e) sph stack pointer high $3d ($5d) spl stack pointer low $3b ($5b) gimsk general interrupt mask register $3a ($5a) gifr general interrupt flag register $39 ($59) timsk timer/counter interrupt mask register $38 ($58) tifr timer/counter interrupt flag register $37 ($57) spmcr store program memory control register $36 ($56) emcucr extended mcu general control register $35 ($55) mcucr mcu general control register $34 ($54) mcusr mcu general status register $33 ($53) tccr0 timer/counter0 control register $32 ($52) tcnt0 timer/counter0 (8-bit) $31 ($51) ocr0 timer/counter0 output compare register $30 ($50) sfior special function io register $2f ($4f) tccr1a timer/counter1 control register a $2e ($4e) tccr1b timer/counter1 control register b $2d ($4d) tcnt1h timer/counter1 high byte $2c ($4c) tcnt1l timer/counter1 low byte $2b ($4b) ocr1ah timer/counter1 output compare registera high byte $2a ($4a) ocr1al timer/counter1 output compare registera low byte $29 ($49) ocr1bh timer/counter1 output compare registerb high byte $28 ($48) ocr1bl timer/counter1 output compare registerb low byte $27 ($47) tccr2 timer/counter2 control register $26 ($46) assr asynchronous mode status register $25 ($45) icr1h timer/counter1 in put capture register high byte $24 ($44) icr1l timer/counter1 in put capture register low byte $23 ($43) tcnt2 timer/counter2 (8-bit) $22 ($42) ocr2 timer/counter2 output compare register $21 ($41) wdtcr watchdog timer control register $20 ($40) ubrrhi uart baud register high $1f ($3f) eearh eeprom ad dress register high $1e ($3e) eearl eeprom address register low $1d ($3d) eedr eeprom data register
20 atmega161(l) 1228d?avr?02/07 note: 1. reserved and unused locations are not shown in this table. $1c ($3c) eecr eeprom control register $1b($3b) porta data register, port a $1a ($3a) ddra data direction register, port a $19 ($39) pina input pins, port a $18 ($38) portb data register, port b $17 ($37) ddrb data direction register, port b $16 ($36) pinb input pins, port b $15 ($35) portc data register, port c $14 ($34) ddrc data direction register, port c $13 ($33) pinc input pins, port c $12 ($32) portd data register, port d $11 ($31) ddrd data direction register, port d $10 ($30) pind input pins, port d $0f ($2f) spdr spi i/o data register $0e ($2e) spsr spi status register $0d ($2d) spcr spi control register $0c ($2c) udr0 uart0 i/o data register $0b ($2b) ucsr0a uart0 control and status register $0a ($2a) ucsr0b uart0 control and status register $09 ($29) ubrr0 uart0 baud rate register $08 ($28) acsr analog comparator control and status register $07 ($27) porte data register, port e $06 ($26) ddre data direction register, port e $05 ($25) pine input pins, port e $03 ($23) udr1 uart1 i/o data register $02 ($22) ucsr1a uart1 control and status register $01 ($21) ucsr1b uart1 control and status register $00 ($20) ubrr1 uart1 baud rate register table 1. atmega161 i/o space (1) (continued) i/o address (sram address) name function
21 atmega161(l) 1228d?avr?02/07 all atmega161 i/os and peripherals are placed in the i/o space. the i/o locations are accessed by the in and out instructions transferring data between the 32 general pur- pose working registers and the i/o space. i/o registers within the address range $00 - $1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. refer to the instruction set section for more details. when using the i/o specific commands in and out, the i/o addresses $00 - $3f must be used. when addressing i/o registers as sram, $20 must be added to this address. all i/o register addresses throughout this document are shown with the sram address in parentheses. for compatibility with futu re devices, reserved bits should be written to ze ro if accessed. reserved i/o memory addresses should never be written. some of the status flags are cleared by writing a logical ?1? to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instructions work with reg- isters $00 to $1f only. the i/o and peripherals control registers are explained in the following sections. status register ? sreg the avr status register (sreg) at i/o space location $3f ($5f) is defined as: bit 7 ? i: global interrupt enable the global interrupt enable bit must be set (one) for the interrupts to be enabled. the individual interrupt enable control is then performed in separate control registers. if the global interrupt enable bit is cleared (zero), none of the interrupts are enabled indepen- dent of the individual interrupt enable settings. the i-bit is cleared by hardware after an interrupt has occurred and is set by the reti instruction to enable subsequent interrupts. bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (bit store) use the t-bit as source and destination for the operated bit. a bit from a register in the register file can be cop- ied into t by the bst instruction and a bit in t can be copied into a bit in a register in the register file by the bld instruction. bit 5 ? h: half carry flag the half carry flag h indicates a half carr y in some arithmetic operations. see the instruction set description for detailed information. bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s comple- ment overflow flag v. see the instruction set description for detailed information. bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetics. see the instruction set descripti on for detailed information. bit 76543210 $3f ($5f) ithsvnzcsreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
22 atmega161(l) 1228d?avr?02/07 bit 2 ? n: negative flag the negative flag n indicates a negative result after the different arithmetic and logic operations. see the instruction set description for detailed information. bit 1 ? z: zero flag the zero flag z indicates a zero result after the different arithmetic and logic opera- tions. see the instruction set description for detailed information. bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the instruction set description for detailed information. note that the status register is not automatically stored when entering an interrupt rou- tine and restored when returning from an interrupt routine. this must be handled by software. stack pointer ? sp the atmega161 stack pointer is implemented as two 8-bit registers in the i/o space locations $3e ($5e) and $3d ($5d). as the atmega161 supports up to 64-kbyte mem- ory, all 16 bits are used. the stack pointer points to the data sram stack area where the subroutine and inter- rupt stacks are located. this stack space in the data sram must be defined by the program before any subroutine calls are executed or interrupts are enabled. the stack pointer must be set to point above $60. the stack pointer is decremented by 1 when data is pushed onto the stack with the push instruction, and it is decremented by 2 when an address is pushed onto the stack with subroutine calls and interrupts. the stack pointer is incremented by 1 when data is popped from the stack with the pop instruction, and it is incremented by 2 when an address is popped from the stack with return from subroutine ret or return from interrupt (reti). reset and interrupt handling the atmega161 provides 20 different interrupt sources. these interrupts and the sepa- rate reset vector each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits that must be set (one) together with the i-bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are automatically defined as the reset and interrupt vectors. the complete list of vectors is shown in table 2. the list also determines the priority levels of the different interrupts. the lower the address, the higher the priority level. reset has the hi ghest priority, and next is int0 (the external interrupt request 0) and so on. bit 151413121110 9 8 $3e ($5e) sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 sph $3d ($5d) sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000
23 atmega161(l) 1228d?avr?02/07 note: 1. if bootrst fuse is programmed, the re set vector is located on program address $1e00, see table 39 on page 112 for details. the most typical and general program setup for the reset and interrupt vector addresses are: address labels code comments $000 jmp reset ; reset handler $002 jmp ext_int0 ; irq0 handler $004 jmp ext_int1 ; irq1 handler $006 jmp ext_int2 ; irq2 handler $008 jmp tim2_comp ; timer2 compare handler $00a jmp tim2_ovf ; timer2 overflow handler $00c jmp tim1_capt ; timer1 capture handler $00e jmp tim1_compa ; timer1 comparea handler $010 jmp tim1_compb ; timer1 compareb handler $012 jmp tim1_ovf ; timer1 overflow handler $014 jmp tim0_comp ; timer0 compare handler $016 jmp tim0_ovf ; timer0 overflow handler $018 jmp spi_stc; ; spi transfer complete handler table 2. reset and interrupt vectors (1) vector no. program address sour ce interrupt definition 1 $000 reset external pin, power-on reset and watchdog reset 2 $002 int0 external interrupt request 0 3 $004 int1 external interrupt request 1 4 $006 int2 external interrupt request 2 5 $008 timer2 comp timer/counter2 compare match 6 $00a timer2 ovf timer/counter2 overflow 7 $00c timer1 capt timer/counter1 capture event 8 $00e timer1 compa timer/counter1 compare match a 9 $010 timer1 compb timer/counter1 compare match b 10 $012 timer1 ovf timer/counter1 overflow 11 $014 timer0 comp timer/counter0 compare match 12 $016 timer0 ovf timer/counter0 overflow 13 $018 spi, stc serial transfer complete 14 $01a uart0, rx uart0, rx complete 15 $01c uart1, rx uart1, rx complete 16 $01e uart0, udre uart0 data register empty 17 $020 uart1, udre uart1 data register empty 18 $022 uart0, tx uart0, tx complete 19 $024 uart1, tx uart1, tx complete 20 $026 ee_rdy eeprom ready 21 $028 ana_comp analog comparator
24 atmega161(l) 1228d?avr?02/07 $01a jmp uart_rxc0 ; uart0 rx complete handler $01c jmp uart_rxc1 ; uart1 rx complete handler $01e jmp uart_dre0 ; udr0 empty handler $020 jmp uart_dre1 ; udr1 empty handler $022 jmp uart_txc0 ; uart0 tx complete handler $024 jmp uart_txc1 ; uart1 tx complete handler $026 jmp ee_rdy ; eeprom ready handler $028 jmp ana_comp ; analog comparator handler ; $02a main: ldi r16,high(ramend) ; main program start $02b out sph,r16 $02c ldi r16,low(ramend) $02d out spl,r16 $02e xxx ? ? ? ? when the bootrst fuse is programmed, the most typical and general program setup for the reset and interrupt vector addresses are: address labels code comments .org $002 ; reset is located at $1e000 $002 jmp ext_int0 ; irq0 handler $004 jmp ext_int1 ; irq1 handler $006 jmp ext_int2 ; irq2 handler $008 jmp tim2_comp ; timer2 compare handler $00a jmp tim2_ovf ; timer2 overflow handler $00c jmp tim1_capt ; timer1 capture handler $00e jmp tim1_compa ; timer1 comparea handler $010 jmp tim1_compb ; timer1 compareb handler $012 jmp tim1_ovf ; timer1 overflow handler $014 jmp tim0_comp ; timer0 compare handler $016 jmp tim0_ovf ; timer0 overflow handler $018 jmp spi_stc; ; spi transfer complete handler $01a jmp uart_rxc0 ; uart0 rx complete handler $01c jmp uart_rxc1 ; uart1 rx complete handler $01e jmp uart_dre0 ; udr0 empty handler $020 jmp uart_dre1 ; udr1 empty handler $022 jmp uart_txc0 ; uart0 tx complete handler $024 jmp uart_txc1 ; uart1 tx complete handler $026 jmp ee_rdy ; eeprom ready handler $028 jmp ana_comp ; analog comparator handler ; $02a main: ldi r16,high(ramend); main program start $02b out sph,r16 $02c ldi r16,low(ramend) $02d out spl,r16 $02e xxx ; .org $1e00 $1e00 jmp reset ; reset handler ? ? ? ?
25 atmega161(l) 1228d?avr?02/07 reset sources the atmega161 has three sources of reset:  power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ).  external reset. the mcu is reset when a low level is present on the reset pin for more than 500 ns.  watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled. during reset, all i/o registers are then set to their initial values and the program starts execution from address $000. the instruction placed in address $000 must be a jmp (relative jump) instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used and regular program code can be placed at these locations. the circuit diagra m in figure 24 shows the reset logic. table 3 and table 4 define the timing and electrical parameters of the reset circuitry. figure 24. reset logic mcu status register (mcusr) delay counters cksel[2:0] ck full porf extrf wdrf data b u s
26 atmega161(l) 1228d?avr?02/07 note: 1. the power-on reset will not work unless the supply voltage has been below v pot (falling). ? notes: 1. the cksel fuses control only the start- up time. the oscillator is the same for all selections. on power-up, the real-time part of the start-up time is increased with typ. 0.6 ms. 2. external power-on reset. 3. table 4 shows the start-up times from reset. from sleep, only the clock counting part of the start-up time is used. the watch dog oscillator is used for timing the real- time part of the start-up time. the number wdt oscillator cycles used for each time- out is shown in table 5. the frequency of the watchdog oscillator is voltage-depend ent as shown in the electri- cal characteristics section. the device is shipped with cksel = 010. table 3. reset characteristics (v cc = 5.0v) (1) symbol parameter min typ max units v pot power-on reset threshold voltage (rising) 1.0 1.4 1.8 v power-on reset threshold voltage (falling) (1) 0.4 0.6 0.8 v v rst reset pin threshold voltage 0.85 v cc v table 4. reset delay selections (3) cksel [2:0] start-up time, v cc = 2.7v, sut unprogrammed start-up time, v cc = 4.0v, sut programmed recommended usage (1) 000 4.2 ms + 6 ck 5.8 ms + 6 ck external clock, fast rising power 001 30 s + 6 ck 10 s + 6 ck external clock (2) 010 67 ms + 16k ck 92 ms + 16k ck crystal oscillator, slowly rising power 011 4.2 ms + 16k ck 5.8 ms + 16k ck crystal oscillator, fast rising power 100 30 s + 16k ck 10 s + 16k ck crystal oscillator (2) 101 67 ms + 1k ck 92 ms + 1k ck ceramic resonator/external clock, slowly rising power 110 4.2 ms + 1k ck 5.8 ms + 1k ck ceramic resonator, fast rising power 111 30 s + 1k ck 10 s + 1k ck ceramic resonator (2) table 5. number of watchdog oscillator cycles sut time-out number of cycles unprogrammed 4.2 ms (at v cc = 2.7v) 1k unprogrammed 67 ms (at v cc = 2.7v) 16k programmed 5.8 ms (at v cc = 4.0v) 4k programmed 92 ms (at v cc = 4.0v) 64k
27 atmega161(l) 1228d?avr?02/07 power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detec- tion level is nominally 1.4v (rising v cc ). the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reach- ing the power-on reset threshold voltage invokes a delay counte r, which determines the delay, for which the device is kept in reset after v cc rise. the time-out period of the delay counter can be defined by the user through the cksel fuses. the eight differ- ent selections for the delay period are presented in table 4. the reset signal is activated again, without any delay, when the v cc decreases below detection level. figure 25. mcu start-up, reset tied to v cc figure 26. mcu start-up, reset controlled externally vcc reset time-out internal reset t tout v pot v rst vcc reset time-out internal reset t tout v pot v rst
28 atmega161(l) 1228d?avr?02/07 external reset an external reset is generated by a low level on the reset pin. reset pulses longer than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage (v rst ) on its positive edge, the delay timer starts the mcu after the time-out period (t tout ) has expired. figure 27. external reset during operation watchdog reset when the watchdog times out, it will generate a short rese t pulse of 1 xtal cycle dura- tion. on the falling edge of this pulse, the delay timer starts counting the time-out period (t tout ). refer to page 58 for details on operation of the watchdog. figure 28. watchdog reset during operation
29 atmega161(l) 1228d?avr?02/07 mcu status register ? mcusr the mcu status register provides information on which reset source caused an mcu reset.  bits 7..4 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is cleared by a power-on reset or by writing a logical ?0? to the flag. bit 2 ? res: reserved bit this bit are reserved bit in the atmega161 and always read as zero. bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is cleared by a power-on reset or by writing a logical ?0? to the flag. bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is cleared only by writing a logical ?0? to the flag. to make use of the reset flags to identify a reset condition, the user should read and then clear the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. interrupt handling the atmega161 has two 8-bit interrupt mask control registers; gimsk (general inter- rupt mask register) and timsk (timer/counter interrupt mask register). when an interrupt occurs, the global interrupt enable i-bit is cleared (zero) and all inter- rupts are disabled. the user software can set (one) the i-bit to enable nested interrupts. the i-bit is set (one) when a return from interrupt instruction (reti) is executed. when the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, hardware clears the corresponding flag that generated the interrupt. some of the interrupt flags can also be cleared by writing a logical ?1? to the flag bit position(s) to be cleared. if an interrupt condition occurs when the corresponding interrupt enable bit is cleared (zero), the interrupt flag will be set and remembered until the interrupt is e nabled or the flag is cleared by software. if one or more interrupt conditions occur when the global interrupt enable bit is cleared (zero), the correspondi ng interrupt fl ag(s) will be set and reme mbered until the global interrupt enable bit is se t (one), and will be execut ed by order of priority. note that external level interrupt does not have a flag and will only be remembered for as long as the interrupt condition is present. bit 76543210 $34 ($54) ? ? ? ? wdrf ? extrf porf mcusr read/write r r r r r/w r r/w r/w initial value 0 0 0 0 see bit description
30 atmega161(l) 1228d?avr?02/07 note that the status register is not automatically stored when entering an interrupt rou- tine or restored when returning from an interrupt routine. this must be handled by software. interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles minimum. after four clock cycles, the program vector address for the actual interrupt handling routine is executed. during this four-clock-cycle period, the program counter (13 bits) is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi- cycle instruction, this instruction is complete d before the interrupt is served. if an inter- rupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clock cycles. a return from an interrupt handling routine ta kes four clock cycles. during these four clock cycles, the program counter (2 bytes) is popped back from the stack, the stack pointer is incremented by 2, and the i-flag in sreg is set. when avr exits from an interrupt, it will always return to the main program and ex ecute one more instruction before any pending interrupt is served. general interrupt mask register ? gimsk bit 7 ? int1: external interrupt request 1 enable when the int1 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control1 bits 1/0 (isc11 and isc10) in the mcu general control regist er (mcucr) define whether the external interrupt is activated on rising and/or falling edge of the int1 pin or is level-sensed. activity on the pin will cause an interrupt request even if int1 is configured as an output. the corresponding interrupt of external inte rrupt request 1 is executed from program memory address $004. see also ?external interrupts?. bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is enabled. the interrupt sense control0 bits 1/0 (isc01 and isc00) in the mcu general control regist er (mcucr) define whether the external interrupt is activated on rising and/or falling edge of the int0 pin or is level-sensed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the corresponding interrupt of external inte rrupt request 0 is executed from program memory address $002. see also ?external interrupts.?  bit 5 ? int2: external interrupt request 2 enable when the int2 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is activated. the interrupt sense control2 bit (isc02 in the extended mcu control register [emcucr]) de fines whether the external interrupt is activated on rising or falling ed ge of the int2 pin. activity on the pi n will cause an inter- rupt request even if int2 is configured as an output. the corresponding interrupt of external interrupt request 2 is executed from program memory address $006. see also ?external interrupts.? bit 76543210 $3b ($5b) int1 int0 int2 ? ? ? ? ? gimsk read/write r/w r/w r r r r r r initial value 0 0 0 0 0 0 0 0
31 atmega161(l) 1228d?avr?02/07  bits 4..0 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. general interrupt flag register ? gifr bit 7 ? intf1: external interrupt flag1 when an event on the int1 pin triggers an interrupt request, intf1 becomes set (one). if the i-bit in sreg and the int1 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $004. the flag is cleared when the interrupt routine is exe- cuted. alternatively, the flag can be cleared by writing a logical ?1? to it. bit 6 ? intf0: external interrupt flag0 when an event on the int0 pin triggers an interrupt request, intf0 becomes set (one). if the i-bit in sreg and the int0 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $002. the flag is cleared when the interrupt routine is exe- cuted. alternatively, the flag can be cleared by writing a logical ?1? to it. bit 5 ? intf2: external interrupt flag2 when an event on the int2 pin triggers an interrupt request, intf2 becomes set (one). if the i-bit in sreg and the int2 bit in gimsk are set (one), the mcu will jump to the interrupt vector at address $006. the flag is cleared when the interrupt routine is exe- cuted. alternatively, the flag can be cleared by writing a logical ?1? to it.  bits 4..0 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. timer/counter interrupt mask register ? timsk bit 7 ? toie1: timer/counter1 overflow interrupt enable when the toie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 overflow interrupt is enabl ed. the corresponding interrupt (at vector $012) is executed if an overflow in timer/counter1 occurs, i.e., when the tov1 bit is set in the timer/counter interrupt flag register (tifr). bit 6 ? oce1a: timer/counter1 output comparea match interrupt enable when the ocie1a bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 comparea match interrupt is enabled. the corresponding interrupt (at vector $00e) is executed if a compare a match in timer/counter1 occurs, i.e., when the ocf1a bit is set in the timer/counter interrupt flag register (tifr). bit 76543210 $3a ($5a) intf1 intf0 intf2 ? ? ? ? ? gifr read/write r/w r/w r/w r r r r r initial value 0 0 0 0 0 0 0 0 bit 76543210 $39 ($59) toie1 ocie1a ocie1b toie2 ticie1 ocie2 toie0 ocie0 timsk read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
32 atmega161(l) 1228d?avr?02/07 bit 5 ? ocie1b: timer/counter1 output compareb match interrupt enable when the ocie1b bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 compareb match interrupt is enabled. the corresponding interrupt (at vector $010) is executed if a compare b match in timer/counter1 occurs, i.e., when the ocf1b bit is set in the timer/counter interrupt flag register (tifr). bit 4 ? toie2: timer/counter2 overflow interrupt enable when the toie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 overflow interrupt is enabl ed. the corresponding interrupt (at vector $00a) is executed if an overflow in timer/counter2 occurs, i.e., when the tov2 bit is set in the timer/counter interrupt flag register (tifr). bit 3 ? ticie1: timer/counter1 input capture interrupt enable when the ticie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 input capture event interrupt is enabled. the corresponding interrupt (at vector $00c) is executed if a capture-triggering event occurs on pin 31, icp, i.e., when the icf1 bit is set in the timer/counter interrupt flag register (tifr). bit 2 ? ocie2: timer/counter2 output compare match interrupt enable when the ocie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 compare match interrupt is enabled. the corresponding interrupt (at vector $008) is executed if a compare2 match in timer/counter2 occurs, i.e., when the ocf2 bit is set in the timer/counter interrupt flag register (tifr). bit 1 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is set (one) and the i-bit in the status register is set (one), the timer/counter0 overflow interrupt is enabl ed. the corresponding interrupt (at vector $016) is executed if an overflow in timer/counter0 occurs, i.e., when the tov0 bit is set in the timer/counter interrupt flag register (tifr). bit 0 ? ocie0: timer/counter0 output compare match interrupt enable when the ocie0 bit is set (one) and the i-bit in the status register is set (one), the timer/counter0 compare match interrupt is enabled. the corresponding interrupt (at vector $014) is executed if a compare 0 match in timer/counter0 occurs, i.e., when the ocf0 bit is set in the timer/counter interrupt flag register (tifr).
33 atmega161(l) 1228d?avr?02/07 timer/counter interrupt flag register ? tifr bit 7 ? tov1: timer/counter1 overflow flag the tov1 is set (one) when an overflow occurs in timer/counter1. tov1 is cleared by hardware when executing the corresponding in terrupt handling vector. alternatively, tov1 is cleared by writing a logical ?1? to the flag. when the i-bit in sreg, and toie1 (timer/counter1 overflow interrupt enable), and tov1 are set (one), the timer/counter1 overflow interrupt is executed. in pwm mode, this bit is set when timer/counter1 changes counting direction at $0000. bit 6 ? ocf1a: output compare flag 1a the ocf1a bit is set (one) when a compare match occurs between the timer/counter1 and the data in ocr1a (output compare register 1a). ocf1a is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf1a is cleared by writing a logical ?1? to the flag. when the i-bit in sreg and ocie1a (timer/counter1 compare match interrupt a enable) and ocf1a are set (one), the timer/counter1 compare a match interrupt is executed. bit 5 ? ocf1b: output compare flag 1b the ocf1b bit is set (one) when a compare match occurs between the timer/counter1 and the data in ocr1b (output compare register 1b). ocf1b is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf1b is cleared by writing a logical ?1? to the flag. when the i-bit in sreg and ocie1b (timer/counter1 compare match interrupt b enable) and ocf1b are set (one), the timer/counter1 compare b match interrupt is executed. bit 4 ? tov2: timer/counter2 overflow flag the bit tov2 is set (one) when an overflow occurs in timer/counter2. tov2 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov2 is cleared by writing a logical ?1? to the flag. when the sreg i-bit and toie2 (timer/counter2 overflow interrupt enable) and tov2 are set (one), the timer/counter2 overflow interrupt is executed. bit 3 ? icf1: input capture flag 1 the icf1 bit is set (one) to flag an input capture event, indicating that the timer/counter1 value has been transferred to the input capture register (icr1). icf1 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, icf1 is cleared by writing a logical ?1? to the flag. when the sreg i-bit and ticie1 (timer/counter1 input capture interrupt enable) and icf1 are set (one), the timer/counter1 capture interrupt is executed. bit 2 ? ocf2: output compare flag 2 the ocf2 bit is set (one) when a compare match occurs between the timer/counter2 and the data in ocr2 (output compare register 2). ocf2 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf2 is cleared by writing a logical ?1? to the flag. when the i-bit in sreg and ocie2 (timer/counter2 bit 76543210 $38 ($58) tov1 ocf1a ocifb tov2 icf1 ocf2 tov0 ocf0 tifr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
34 atmega161(l) 1228d?avr?02/07 compare match interrupta enable) and the ocf2 are set (one), the timer/counter2 compare match interrupt is executed. bit 1 ? tov0: timer/counter0 overflow flag the bit tov0 is set (one) when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logical ?1? to the flag. when the sreg i-bit and toie0 (timer/counter0 overflow interrupt enable) and tov0 are set (one), the timer/counter0 overflow interrupt is executed. bit 2 ? ocf0: output compare flag 0 the ocf0 bit is set (one) when compare match occurs between the timer/counter0 and the data in ocr0 (output compare register 0). ocf0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf0 is cleared by writing a logical ?1? to the flag. when the i-bit in sreg and ocie0 (timer/counter0 compare match interrupta enable) and the ocf0 are set (one), the timer/counter0 compare match interrupt is executed. external interrupts the external interrupts are triggered by the int0, int1, and int2 pins. observe that, if enabled, the interrupts will tr igger even if the int0/int1/int 2 pins are configured as out- puts. this feature provides a way of generating a software interrupt. the external interrupts can be triggered by a falling or rising edge or a low level (int2 is only an edge triggered interrupt). this is set up as indicated in the specification for the mcu control register ? mcucr (int0/int1) and emcucr (int2). when the external interrupt is enabled and is conf igured as level triggered (only in t0/int1), the inte rrupt will trigger as long as the pin is held low. mcu control register ? mcucr the mcu control register contains control bits for general mcu functions. bit 7 ? sre: external sram enable when the sre bit is set (one), the external data memory interface is enabled and the pin functions ad0 - 7 (port a), a8 - 5 (port c), ale (port e), wr, and rd (port d) are activated as the alternate pin functions. th e sre bit overrides any pin direction settings in the respective data direction registers. see figure 50 through figure 53 for a description of the external memory pin functions. when the sre bit is cleared (zero), the external data memory interface is disabled and the normal pin and data direction settings are used. bit 6 ? srw10: external sram wait state the srw10 bit is used to set up extra wait states in the external memory interface. see ?double-speed transmission? on page 78 for a detailed description. bit 5 ? se: sleep enable the se bit must be set (one) to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the pro- bit 76543210 $35 ($55) sre srw10 se sm1 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
35 atmega161(l) 1228d?avr?02/07 grammer?s purpose, it is recommended to set the sleep enable (se) bit just before the execution of the sleep instruction. bit 4 ? sm1: sleep mode select bit 1 the sm1 bit, together with th e sm0 control bit in emcucr, selects between the three available sleep modes as shown in table 6.  bits 3, 2 ? isc11, isc10: interrupt sense control 1 bit 1 and bit 0 the external interrupt 1 is activated by the ex ternal pin int1 if the sreg i-flag and the corresponding interrupt mask in the gimsk are set. the level and edges on the external int1 pin that activate the interrupt are def ined in table 7. the value on the int1 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pu lses are not guaran- teed to generate an interrupt. if low-level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt.  bits 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the ex ternal pin int0 if the sreg i-flag and the corresponding interrupt mask is set. the level and edges on the external int0 pin that activate the interrupt are defined in table 8. the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low-level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. table 6. sleep mode select sm1 sm0 sleep mode 00idle 01reserved 10power-down 11power-save table 7. interrupt 1 sense control isc11 isc10 description 0 0 the low level of int1 generates an interrupt request. 0 1 any logical change on int1 generates an interrupt request. 1 0 the falling edge of int1 generates an interrupt request. 1 1 the rising edge of int1 generates an interrupt request. table 8. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request.
36 atmega161(l) 1228d?avr?02/07 extended mcu control register ? emcucr the extended mcu control register contains c ontrol bits for external interrupt 2, sleep mode bit and control bits for the external memory interface. bit 7 ? sm0: sleep mode bit 0 when this bit is set (one) and sleep mode bit 1 (sm1) in mcucr is set, power-save mode is selected as sleep mode. refer to page 36 for a detailed description of the sleep modes.  bits 6..4 ? srl2, srl1, srl0: external sram limit it is possible to configure different wait states for different external memory addresses in atmega161. the srl2 - srl0 bits are used to define at which address the different wait states will be configured. see ?interfa ce to external memory? on page 84 for a detailed description.  bits 3..1 ? srw01, srw00, srw11: external sram wait state select bits the srw01, srw00 and srw11 bits are used to set up extra wait states in the exter- nal memory interface. see ?interface to external memory? on page 84 for a detailed description. bit 0 ? isc2: interrupt sense control 2 the external interrupt 2 is activated by the ex ternal pin int2 if the sreg i-flag and the corresponding interrupt mask in the gimsk are set. if isc2 is cleared (zero), a falling edge on int2 activates the interrupt. if isc2 is set (one), a rising edge on int2 activates the interrupt. edges on int2 are registered asynchronously. pulses on int2 wider than 50 ns will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. when changing the isc2 bit, an interrupt can occur. therefore, it is recommended to first disable int2 by clearing its interrupt enable bit in the gimsk register. then, isc2 bit can be changed. finally, the int2 interrupt flag should be cleared by writing a logical ?1? to its interrupt flag bit in the gifr register before the interrupt is re-enabled. sleep modes to enter any of the three sleep modes, th e se bit in mcucr must be set (one) and a sleep instruction must be exec uted. the sm1 bit in the m cucr register and sm0 bit in the emcucr register select which sleep mode (idle, power-down or power-save) will be activated by the sleep instructi on (see table 6). if an enabled interrupt occurs while the mcu is in a sleep mode, the mcu awakes. the cpu is then halted for four cycles, it executes the interrupt routine, and resumes execution from the instruction fol- lowing sleep. the contents of the register file, sram an d i/o memory are unaltered. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. idle mode when the sm1/sm0 bits are set to 00, the sleep instruction makes the mcu enter the idle mode, stopping the cpu but allowing spi, uarts, analog comparator, timer/counters, watchdog, and the interrupt system to continue operating. this enables the mcu to wake up from external tr iggered interrupts as well as internal ones like the timer overflow and uart receive complete interrupts. if wake-up from the bit 76543210 $36 ($56) sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc2 emcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
37 atmega161(l) 1228d?avr?02/07 analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in the analog comparator control and status register (acsr). this will reduce power consumption in idle mode. power-down mode when the sm1/sm0 bits are set to 10, the sleep instruction makes the mcu enter the power-down mode. in this mo de, the external oscillator is stopped while the external interrupts and the watchdog (if enabled) conti nue operating. only an external reset, a watchdog reset (if enabled), an external level interrupt on int0 or int1, or an external edge interrupt on int2 can wake up the mcu. if int2 is used for wake-up from power-down mode, the edge is remembered until the mcu wakes up. if a level-triggered interrupt is used for wake-up from power-down mode, the changed level must be held for some time to wake up the mcu. this make s the mcu less sensi- tive to noise. the changed level is sample d twice by the watchdog oscillator clock, and if the input has the required level during this time, the mcu will wake up. the period of the watchdog oscillator is 1 s (nominal) at 5.0v and 25 c. the frequency of the watchdog oscillator is voltage -dependent as shown in the electrical characteristics section. when waking up from power-down mode, a delay from the wake-up condition occurs until the wake-up becomes effective. this allows the clock to restart and become stable after having been st opped. the wake-up period is de fined by the sa me cksel fuses that define the reset time-out period. the wake-up period is equal to the clock counting part of the reset period, as shown in table 4. if the wake-up condition disappears before the mcu wakes up and starts to execute, e.g., a low level on int0 is not held long enough, the interrupt causing th e wake-up will not be executed. power-save mode when the sm1/sm0 bits are 11, the sleep instruction makes the mcu enter the power-save mode. this mode is identica l to power-down, with one exception. if timer/counter2 is clocked asynchronously, i.e., the as2 bit in assr is set, timer/counter2 will run during sleep. in ad dition to the power- down wake-up sources, the device can also wake up from either timer overflow or output compare event from timer/counter2 if the corresponding timer/counter2 interrupt enable bits are set in timsk and the global interrupt enable bit in sreg is set. if the asynchronous timer is not clocked asynchronously, po wer-down mode is recom- mended instead of power-save mode because the contents of the register in the asynchronous timer should be considered undefined after wake-up in power-save mode even if as2 is 0.
38 atmega161(l) 1228d?avr?02/07 timer/counters the atmega161 provides three general purpose timer/counters ? two 8-bit t/cs and one 16-bit t/c. timer/counter2 can optionally be asynchronously clocked from an exter- nal oscillator. this oscillator is optimiz ed for use with a 32.768 khz watch crystal, enabling use of timer/counter2 as a real-time clock (rtc). timer/counters 0 and 1 have individual prescaling selection from the same 10-bit prescaling timer. timer/counter2 has its own prescaler. both these prescalers can be reset by setting the corresponding control bits in the special functions io register (sfior). refer to page 39 for a detailed description. these timer/counters can either be used as a timer with an internal clock time base or as a counter with an external pin connection, which triggers the counting. timer/counter prescalers figure 29. prescaler for timer/counter0 and 1 for timer/counters 0 and 1, the four prescaled selections are: ck/8, ck/64, ck/256, and ck/1024, where ck is the oscillator clock. for the two timer/counters 0 and 1, ck, external source and stop can also be select ed as clock sources. setting the psr10 bit in sfior resets the prescaler. this allows the user to operate with a predictable pres- caler. note that timer/counter1 and timer/counter0 share the same prescaler and a prescaler reset will affect both timer/counters. psr10 clear tck1 tck0
39 atmega161(l) 1228d?avr?02/07 figure 30. timer/counter2 prescaler the clock source for timer/counter2 prescaler is named pck2. pck2 is by default con- nected to the main system clock (ck). by setting the as2 bit in assr, timer/counter2 is asynchronously clocked from the pd4(tosc1) pin. this enables use of timer/counter2 as a real-time clock (rtc). when as2 is set, pins pd4(tosc1) and pd5(tosc2) are disconnected from port d. a crystal can then be connected between the pd4(tosc1) and pd5(tosc2) pins to serve as an independent clock source for timer/counter2. the oscillator is optimized for use with a 32.768 khz crystal. alterna- tively, an external clock signal can be a pplied to pd4(tosc1). the frequency of this clock must be lower than one fourth of the cpu clock and not higher than 256 khz. set- ting the psr2 bit in sfior resets the prescaler. this allows the user to operate with a predictable prescaler. special function io register ? sfior  bits 7..2 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 1 ? psr2: prescaler reset timer/counter2 when this bit is set (one), the timer/co unter2 prescaler will be reset. the bit will be cleared by hardware after the operation is pe rformed. writing a zero to this bit will have no effect. this bit will always be read as zero if timer/counter2 is clocked by the internal cpu clock. if this bit is written when timer/counter2 is operating in asynchronous mode, however, the bit will rema in as one until the prescale r has been reset. see ?asyn- chronous operation of timer/counter2? on page 48 for a detailed description of asynchronous operation. 10-bit t/c prescaler timer/counter2 clock source ck pck2 tosc1 as2 cs20 cs21 cs22 pck2/8 pck2/64 pck2/128 pck2/1024 pck2/256 pck2/32 0 psr2 clear tck2 bit 765432 1 0 $30 ($50) ??????psr2psr10sfior read/writerrrrrr r/w r/w initial value 0 0 0 0 0 0 0 0
40 atmega161(l) 1228d?avr?02/07 bit 0 ? psr10: prescaler reset time r/counter1 and timer/counter0 when this bit is set (one), the timer/counter1 and timer/counter0 prescaler will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. note that timer/counter1 and timer/counter0 share the same prescaler and a reset of this prescaler will af fect both timers. this bit will always be read as zero. 8-bit timer/counters t/c0 and t/c2 figure 31 shows the block diagram for timer/ counter0. figure 32 shows the block dia- gram for timer/counter2. figure 31. timer/counter0 block diagram 8-bit data bus timer int. flag register (tifr) timer/counter0 (tcnt0) 8-bit comparator output compare register0 (ocr0) timer int. mask register (timsk) 0 0 0 7 7 7 t/c clk source up/down t/c clear control logic tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 ck t/c0 over- flow irq t/c0 compare match irq ocf0 tov0 toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocie0 t/c0 control register (tccr0) cs02 com01 pwm0 cs01 com00 cs00 ctc0 foc0 psr2 psr10 special functions io register (sfior) t0
41 atmega161(l) 1228d?avr?02/07 figure 32. timer/counter2 block diagram the 8-bit timer/counter0 can select clock source from ck, prescaled ck or an external pin.the 8-bit timer/counter2 can select clock source from ck, prescaled ck or external tosc1. both timer/counters can be stopped as described in sections ?timer/counter0 control register ? tccr0? and ?timer/counte r2 control register ? tccr2?. the various status flags (overflow and compare match) are found in the timer/counter interrupt flag register (tifr). control signals are found in the timer/counter control regist er (tccr0 and tccr2). the interrupt enable/disable set- tings are found in the timer/counter interrupt mask register (timsk). when timer/counter0 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 8-bit timer/counters feature both a high resolution and a high accuracy usage with the lower prescaling opportunities. similarly, the high prescaling opportunities make the timer/counter0 useful for lower speed functi ons or exact timing functions with infre- quent actions. timer/counters 0 and 2 can also be used as 8-bit pulse width modulators. in this mode, the timer/counter and the output compare register serve as a glitch-free, stand-alone pwm with centered pulses. refe r to page 44 for a detailed description of this function. 8-bit data bus 8-bit asynch t/c2 data bus asynch. status register (assr) timer int. flag register (tifr) timer/counter2 (tcnt2) synch unit 8-bit comparator output compare register2 (ocr2) timer int. mask register (timsk) 0 0 0 7 7 7 t/c clk source up/down t/c clear control logic ocf2 tov2 toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocr2ub tc2ub icr2ub tosc1 ck tck2 t/c2 over- flow irq t/c2 compare match irq as2 tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 ocie0 t/c2 control register (tccr2) cs22 com21 pwm2 cs21 com20 cs20 ctc2 foc2 psr2 psr10 special functions io register (sfior) ck
42 atmega161(l) 1228d?avr?02/07 timer/counter0 control register ? tccr0 timer/counter2 control register ? tccr2 bit 7 ? foc0/foc2: force output compare writing a logical ?1? to this bit forces a change in the compare match output pin pb0 (timer/counter0) and pb1 (timer/counter2) according to the values already set in comn1 and comn0. if the comn1 and comn0 bits are written in the same cycle as foc0/foc2, the new settings will not take effect until the next compare match or forced output compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in comn1 and comn0 happens as if a compare match had occurred, but no interrupt is generated and the timer/coun ters will not be cleared even if ctc0/ctc2 is set. the foc0/foc2 bits will always be read as zero. the setting of the foc0/foc2 bits has no effect in pwm mode. bit 6 ? pwm0/pwm2: pulse width modulator enable when set (one), this bit enables pwm mode for timer/counter0 or timer/counter2. this mode is described on page 44.  bits 5, 4 ? com01, com00/com21, com20: compare output mode, bits 1 and 0 the comn1 and comn0 control bits determine any output pin action following a com- pare match in timer/counter0 or timer/counter2. output pin actions affect pins pb0(oc0) or pb1(oc2). this is an alternat ive function to an i/o port and the corre- sponding direction control bit must be set (one) to control an output pin. the control configuration is shown in table 9. notes: 1. in pwm mode, these bits have a differ ent function. refer to table 12 for a detailed description. 2. n = 0 or 2 bit 3 ? ctc0/ctc2: clear timer/counter on compare match when the ctc0 or ctc2 control bit is set (o ne), timer/counter0 or timer/counter2 is reset to $00 in the cpu clock cycle after a compare match. if the control bit is cleared, the timer/counter continues counting and is unaffected by a compare match. when a bit 76543210 $33 ($53) foc0 pwm0 com01 com00 ctc0 cs02 cs01 cs00 tccr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $27 ($47) foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 tccr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 9. compare mode select (1)(2) comn1 comn0 description 0 0 timer/counter disconnected from output pin ocn. 0 1 toggle the ocn output line. 1 0 clear the ocn output line (to zero). 1 1 set the ocn output line (to one).
43 atmega161(l) 1228d?avr?02/07 prescaling of 1 is used, and the compare register is set to c, the timer will count as fol- lows if ctc0/ctc2 is set: ... | c-1 | c | 0 | 1 | ... when the prescaler is set to divide by 8, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 | 1, 1, 1, ... in pwm mode, this bit has a different function. if the ctc0 or ctc2 bit is cleared in pwm mode, the timer/counter acts as an up/down counter. if the ctc0 or ctc2 bit is set (one), the timer/counter wraps when it reaches $ff. refer to page 44 for a detailed description.  bits 2, 1, 0 ? cs02, cs01, cs00/cs22, cs21, cs20: clock select bits 2, 1 and 0 the clock select bits 2, 1, and 0 define the prescaling source of timer/counter0 and timer/counter2. the stop condition provides a timer enable/disable function. the prescaled modes are scaled directly from the ck oscillator clock for timer/count er0 and pck2 for timer/counter2. if the external pin modes are used for timer/counter0, transitions on pb0/(t0) will clock the counter even if the pin is configured as an output. this feature can give the user software control of the counting. table 10. clock 0 prescale select cs02 cs01 cs00 description 0 0 0 stop, the timer/counter0 is stopped. 001ck 010ck/8 011ck/64 100ck/256 101ck/1024 1 1 0 external pin pb0(t0), falling edge 1 1 1 external pin pb0(t0), rising edge table 11. clock 2 prescale select cs22 cs21 cs20 description 0 0 0 stop, the timer/counter2 is stopped. 0 0 1 pck2 0 1 0 pck2/8 0 1 1 pck2/32 1 0 0 pck2/64 1 0 1 pck2/128 1 1 0 pck2/256 1 1 1 pck2/1024
44 atmega161(l) 1228d?avr?02/07 timer counter0 ? tcnt0 timer/counter2 ? tcnt2 these 8-bit registers contain the value of the timer/counters. both timer/counters are realized as up or up/down (in pwm mode) counters with read and write access. if the timer/co unter is written to and a cloc k source is selected, it con- tinues counting in the timer clock cycle following the write operation. timer/counter0 output compare register ? ocr0 timer/counter2 output compare register ? ocr2 the output compare registers are 8-bit read/write registers. the timer/counter output compare registers contain the data to be continuously compared with the timer/counter. actions on compare matche s are specified in tccr0 and tccr2. a software write to the timer/counter register blocks compare matches in the next timer/counter clock cycle. this prevents immediate interrupts when initializing the timer/counter. a compare match will set the compare interr upt flag in the cpu clock cycle following the compare event. timer/counters 0 and 2 in pwm mode when pwm mode is selected, the timer/counter either wraps (overflows) when it reaches $ff or it acts as an up/down counter. if the up/down mode is selected, the timer/counter and the output compare registers (ocr0 or ocr2) form an 8-bit, free-running, glitch-free and phase-correct pwm with outputs on the pb0(oc0/pwm0) or pb1(oc2/pwm2) pin. if the overflow mode is selected, the timer/counter and the output compare registers (ocr0 or ocr2) form an 8-bit, free-running and glitch-free pwm, operating with twice the speed of the up/down counting mode. bit 76543210 $32 ($52) msb lsb tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $23 ($43) msb lsb tcnt2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $31 ($51) msb lsb ocr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $22 ($42) msb lsb ocr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
45 atmega161(l) 1228d?avr?02/07 pwm modes (up/down and overflow) the two different pwm modes are selected by the ctc0 or ctc2 bit in the timer/counter control registers ? tccr0 or tccr2, respectively. if ctc0/ctc2 is cleared and pwm mode is se lected, the timer/counter acts as an up/down counter, counting up from $00 to $ff, where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the output compare register, the pb0(oc0/ pwm0) or pb1(oc2/pwm2) pin is set or cleared according to the settings of the comn1/comn0 bits in the timer/counter con- trol registers tccr0 or tccr2. if ctc0/ctc2 is set and pwm mode is sele cted, the timer/counter s will wrap and start counting from $00 after reaching $ff. the pb0(oc0/pwm0) or pb1(oc2/pwm2) pin will be set or cleared according to the settings of comn1/comn0 on a timer/counter overflow or when the counter value matches the contents of the output compare regis- ter. refer to table 12 for details. note: 1. n = 0 or 2 note that in pwm mode, the value to be written to the output compare register is first transferred to a temporary location and then latched into the ocr when the timer/counter reaches $ff. this prevent s the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr0 or ocr2 write. see figure 33 and figure 34 for examples. table 12. compare mode select in pwm mode (1) ctcn comn1 comn0 effect on compare pin frequency 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). f tck0/2 /510 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). f tck0/2 /510 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow f tck0/2 /256 1 1 1 set on compare match, cleared on overflow f tck0/2 /256
46 atmega161(l) 1228d?avr?02/07 figure 33. effects of unsynchronized ocr latching in up/down mode figure 34. effects of unsynchronized ocr latching in overflow mode. note: n = 0 or 2 (figure 33 and figure 34) during the time between the write and the latch operation, a read from the output com- pare registers will read the contents of the te mporary location. th is means that the most recently written value alwa ys will read out of ocr0 and ocr2. when the output compare register contains $00 or $ff, and the up/down pwm mode is selected, the output pb0(oc0/pwm0)/pb1(oc2/pwm2) is updated to low or high on the next compare match according to the settings of comn1/comn0. this is shown in table 13. in overflow pwm mode, the output pb0(oc0/pwm0)/pb1(oc2/pwm2) is held low or high only when the output compare register contains $ff. note: 1. n = 0 or 2 2. in overflow pwm mode, the table above is only valid for ocrn = $ff. table 13. pwm outputs ocrn = $00 or $ff (1)(2) comn1 comn0 ocrn output pwmn 1 0 $00 l 10$ff h 1 1 $00 h 11$ff l pwm output ocn pwm output ocn unsynchronized ocn latch synchronized ocn latch compare value changes counter value compare value glitch counter value compare value compare value changes pwm output ocn pwm output ocn unsynchronized ocn latch synchronized ocn latch counter value compare value counter value compare value compare value changes compare value changes glitch
47 atmega161(l) 1228d?avr?02/07 in up/down pwm mode, the timer overflow flag (tov0 or tov2) is set when the counter advances from $00. in overflow pw m mode, the timer overflow flag is set as in normal timer/counter mode. timer overflow interrupt0 and 2 operate exactly as in normal timer/counter mode, i.e., they are executed when tov0 or tov2 are set, pro- vided that timer overflow interrupt and glob al interrupts are enabled. this also applies to the timer output compare flag and interrupt. asynchronous status register ? assr  bits 7..4 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 3 ? as2: asynchronous timer/counter2 mode when this bit is cleared (zero), timer/coun ter2 is clocked from the internal system clock, ck. if as2 is set, the timer/counter2 is clocked from the tosc1 pin. pins pd4 and pd5 become connected to a crystal oscillator and can not be used as general i/o pins. when the value of this bit is change d, the contents of tcnt2, ocr2 and tccr2 might get corrupted. bit 2 ? tcn2ub: timer/counter2 update busy when timer/counter2 operates asynchronous ly and tcnt2 is written, this bit becomes set (one). when tcnt2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical ?0? in this bit indicates that tcnt2 is ready to be updated with a new value. bit 1 ? ocr2ub: output compare register2 update busy when timer/counter2 operates asynchronously and ocr2 is written, this bit becomes set (one). when ocr2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical ?0? in th is bit indicates that ocr2 is ready to be updated with a new value. bit 0 ? tcr2ub: timer/counter control register2 update busy when timer/counter2 operates asynchronously and tccr2 is written, this bit becomes set (one). when tccr2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical ?0? in this bit indicates that tccr2 is ready to be updated with a new value. if a write is performed to any of the three timer/counter2 registers while its update busy flag is set (one), the updated value might get corrupted and cause an uninten- tional interrupt to occur. the mechanisms for reading tcnt2, ocr2 and tccr2 are different. when reading tcnt2, the actual timer value is read. when reading ocr2 or tccr2, the value in the temporary storage register is read. bit 76543 2 1 0 $26 ($46) ? ? ? ? as2 tcn2ub ocr2ub tcr2ub assr read/write r r r r r/w r r r initial value 0 0 0 0 0 0 0 0
48 atmega161(l) 1228d?avr?02/07 asynchronous operation of timer/counter2 when timer/counter2 operates asynchronously, some considerations must be taken:  warning: when switching between asynchronous and synchronous clocking of timer/counter2, the timer registers tcnt2, ocr2, and tccr2 might get corrupted. a safe procedure for switching clock source is: 1. disable the timer/counter2 interrupts by clearing ocie2 and toie2. 2. select clock source by setting as2 as appropriate. 3. write new values to tcnt2, ocr2, and tccr2. 4. to switch to asynchronous operation: wait for tcn2ub, ocr2ub, and tcr2ub. 5. enable interrupts, if needed.  the oscillator is optimized for use with a 32,768 hz watch crystal. an external clock signal applied to this pin goes through the same amplifier having a bandwidth of 256 khz. the external clock signal should therefore be in the interval 0 hz - 256 khz. the frequency of the clock signal applied to the tosc1 pin must be lower than one fourth of the cpu main clock frequency.  when writing to one of the registers tcnt2, ocr2, or tccr2, the value is transferred to a temporary register, and latched after two positive edges on tosc1. the user should not write a new value before the contents of the temporary register have been transferred to its destination. each of the three mentioned registers have their individual temporary register, which means that e.g., writing to tcnt2 does not disturb an ocr2 write in progress. to detect that a transfer to the destination register has taken place, a asynchronous status register ? assr has been implemented.  when entering power-save mode after havi ng written to tcnt 2, ocr2 or tccr2, the user must wait until the written register has been updated if timer/counter2 is used to wake up the device. otherwise, the mcu will go to sleep before the changes have had any effect. this is extremely important if the output compare2 interrupt is used to wake up the device; output compare is disabled during write to ocr2 or tcnt2. if the write cycle is not finished (i.e., the mcu enters sleep mode before the ocr2ub bit returns to zero), the devi ce will never get a co mpare matc h and the mcu will not wake up.  if timer/counter2 is used to wake up the device from power-save mode, precautions must be taken if the user wants to re-enter power-save mode: the interrupt logic needs one tosc1 cycle to be reset. if the time between wake-up and re-entering power-save mode is less t han one tosc1 cycle, the interrupt will not occur and the device will fail to wake up. if the user is in doubt whether the time before re-entering power-save is sufficient, the following algorithm can be used to ensure that one tosc1 cycle has elapsed: 1. write a value to tc cr2, tcnt2, or ocr2. 2. wait until the corresponding update busy flag in assr returns to zero. 3. enter power-save mode.  when asynchronous operation is selected, the 32 khz o scillator for timer/counter2 is always running, except in power-down mode. after a power-up reset or wake-up from power-down, the user shou ld be aware of the fact that this oscillator might take as long as one second to stabilize. the user is advise d to wait for at least one second before using timer/counter2 after power-up or wake-up from power-down. the contents of all timer/counter2 registers must be considered lost after a wake- up from power-down, due to unstable clock signal upon start-up, regardless of whether the oscillator is in use or a cl ock signal is applied to the tosc pin.
49 atmega161(l) 1228d?avr?02/07  description of wake-up from power-save mode when the timer is clocked asynchronously: when the interrupt condition is met, the wake-up process is started on the following cycle of the timer clock, that is, the timer is always advanced by at least 1 before the processor can read the counter value. the interrupt flags are updated three processor cycles after the processor clock has started. during these cycles, the processor executes instructions, but the interrupt condition is not readable and the interrupt routine has not started yet.  during asynchronous operation, the synchronization of the interrupt flags for the asynchronous timer takes thr ee processor cycles plus one timer cycle. the timer is therefore advanced by at least 1 before the processor can read the timer value, causing the setting of the interrupt flag. the output compare pin is changed on the timer clock and is not synchronized to the processor clock. timer/counter1 figure 35 shows the block diagram for timer/counter1. figure 35. timer/counter1 block diagram the 16-bit timer/counter1 can select clock source from ck, prescaled ck or an exter- nal pin. in addition, it can be stopped as described in ?timer/counter1 control register b ? tccr1b?. the different status flags (o verflow, compare match, and capture event) are found in the timer/counter interrupt flag register (tifr). control signals are found in the timer/counter1 control registers (tccr1a and tccr1b). the inter- rupt enable/disable settings for timer/counter1 are found in the timer/counter interrupt mask register (timsk). when timer/counter1 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocie0 tov0 tov1 ocf1a ocf1b icf1 tov2 ocf2 ocf0 timer int. flag register (tifr) control logic timer/counter1 (tcnt1) timer int. mask register (timsk) t/c1 input capture register (icr1) t/c1 over- flow irq t/c1 compare matcha irq t/c1 compare matchb irq t/c1 input capture irq 8-bit data bus 16 bit comparator timer/counter1 output compare register a 16 bit comparator timer/counter1 output compare register b capture trigger ck 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 0 7 8 15 t/c clear t/c clock source up/down t/c1 control register a (tccr1a) t/c1 control register b (tccr1b) foc1a foc1b com1a1 com1a0 com1b1 com1b0 pwm11 pwm10 icnc1 ices1 ctc1 cs12 cs11 cs10 psr2 psr10 special functions io register (sfior) tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf0 tov0 t1
50 atmega161(l) 1228d?avr?02/07 clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 16-bit timer/counter1 features both a high-resolution and a high-accuracy usage with the lower prescaling opportunities. si milarly, the high-prescaling opportunities make the timer/counter1 useful for lower speed functions or exact timing functions with infrequent actions. the timer/counter1 supports two output co mpare functions using the output compare register 1 a and b (ocr1a and ocr1b) as the data sources to be compared to the timer/counter1 contents. the output comp are functions include optional clearing of the counter on comparea match and actions on the output compare pins on both com- pare matches. timer/counter1 can also be used as an 8-, 9-, or 10-bit pulse width modulator. in this mode the counter and the ocr1a/ocr1b registers serve as a dual glitch-free stand- alone pwm with centered pulses. alternativ ely, the timer/counter1 can be configured to operate at twice the speed in pwm mode, but without centered pulses. refer to page 55 for a detailed description of this function. the input capture function of timer/counter1 provides a capture of the timer/counter1 contents to the input capture register (icr1), triggered by an external event on the input capture pin (icp). the actual capture event settings are defined by the timer/counter1 control regist er (tccr1b). in addition, th e analog comparator can be set to trigger the input capture. refer to the section, ?the analog comparator?, for details on this. the icp pin logic is shown in figure 36. figure 36. icp pin schematic diagram if the noise canceler function is enabled, th e actual trigger condition for the capture event is monitored over four samples, and all four must be equal to activate the capture flag. timer/counter1 control register a ? tccr1a  bits 7, 6 ? com1a1, com1a0: compare output mode1a, bits 1 and 0 the com1a1 and com1a0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1a (output comparea pin 1). this is an alternative function to an i/o port, and the corresponding direction control bit must be set (one) to control an output pin. the control configuration is shown in table 14. bit 76543210 $2f ($4f) com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 tccr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
51 atmega161(l) 1228d?avr?02/07  bits 5, 4 ? com1b1, com1b0: compare output mode1b, bits 1 and 0 the com1b1 and com1b0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1b (output compareb). this is an alternative function to an i/o port, and the corresponding direc- tion control bit must be set (one) to control an output pin. the following control configuration is given: note: 1. x = a or b in pwm mode, these bits have a different function. refer to table 18 for a detailed description. bit 3 ? foc1a: force output compare1a writing a logical ?1? to this bit forces a change in the compare match output pin pd5 according to the values already set in com1a1 and com1a0. if the com1a1 and com1a0 bits are written in the same cycle as foc1a, the new settings will not take effect until the next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com1a1 and com1a0 happens as if a compare match had occurred, but no interr upt is generated and it will not clear the timer even if ctc1 in tccr1b is set. the foc1a bit will always be read as zero. the setting of the foc1a bit has no effect in pwm mode. bit 2 ? foc1b: force output compare1b writing a logical ?1? to this bit forces a change in the compare match output pin pe2 according to the values already set in com1b1 and com1b0. if the com1b1 and com1b0 bits are written in the same cycle as foc1b, the new settings will not take effect until the next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com1b1 and com1b0 happens as if a compare match had occurred, but no in terrupt is generated. the foc1b bit will always be read as zero. the setting of the foc1b bit has no effect in pwm mode. table 14. compare 1 mode select (1) com1x1 com1x0 description 0 0 timer/counter1 disconnected from output pin oc1x 0 1 toggle the oc1x output line. 1 0 clear the oc1x output line (to zero). 1 1 set the oc1x output line (to one).
52 atmega161(l) 1228d?avr?02/07  bits 1..0 ? pwm11, pwm10: pulse width modulator select bits these bits select pwm operation of timer/c ounter1 as specified in table 15. this mode is described on page 55. timer/counter1 control register b ? tccr1b bit 7 ? icnc1: input capture1 noise canceler (4 cks) when the icnc1 bit is cleared (zero), the in put capture trigger noise canceler function is disabled. the input capture is triggered at the firs t rising/falling edge sampled on the icp (input capture pin) as specified. when the icnc1 bit is set ( one), four successive samples are measured on the icp (input capture pin), and all samples must be high/low according to the input capture trigger specification in the ices1 bit. the actual sampling frequency is xtal clock frequency. bit 6 ? ices1: input capture1 edge select while the ices1 bit is cleared (zero), the timer/counter1 contents are transferred to the input capture register (icr1) on the falli ng edge of the input capture pin (icp). while the ices1 bit is set (one), the timer/counter1 contents are transferred to the input cap- ture register (icr1) on the rising edge of the input capture pin (icp).  bits 5, 4 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 3 ? ctc1: clear timer/counter1 on compare match when the ctc1 control bit is set (one), the timer/counter1 is reset to $0000 in the clock cycle after a comparea match. if the ctc1 control bit is cleared, timer/counter1 contin- ues counting and is unaffected by a compar e match. when a prescaling of 1 is used and the compare a register is set to c, the timer will count as follows if ctc1 is set: ... | c-1 | c | 0 | 1 | ... when the prescaler is set to divide by 8, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 | ... in pwm mode, this bit has a different functi on. if the ctc1 bit is cleared in pwm mode, the timer/counter1 acts as an up/down counter. if the ctc1 bit is set (one), the timer/counter wraps when it reaches the top value. refer to page 55 for a detailed description. table 15. pwm mode select pwm11 pwm10 description 0 0 pwm operation of timer/counter1 is disabled. 0 1 timer/counter1 is an 8-bit pwm. 1 0 timer/counter1 is a 9-bit pwm. 1 1 timer/counter1 is a 10-bit pwm. bit 76543210 $2e ($4e) icnc1 ices1 ? ? ctc1 cs12 cs11 cs10 tccr1b read/write r/w r/w r r r/w r/w r/w r/w initial value00000000
53 atmega161(l) 1228d?avr?02/07  bits 2, 1, 0 ? cs12, cs11, cs10: clock select1, bits 2, 1 and 0 the clock select1 bits 2, 1 and 0 define the prescaling source of timer/counter1. the stop condition provides a timer en able/disable function. the ck down divided modes are scaled directly from the ck oscill ator clock. if the external pin modes are used for timer/counter1, transi tions on pb1/(t1) will clock t he counter even if the pin is configured as an output. this feature can give the user software control of the counting. timer/counter1 register ? tcnt1h and tcnt1l this 16-bit register contains the presca led value of the 16-bit timer/counter1. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is performed using an 8-bit temporary regis- ter (temp). this temporary register is also used when accessing ocr1a, ocr1b, and icr1. if the main program and interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routines.  tcnt1 timer/counter1 write when the cpu writes to the high byte tcnt1h, the written data is placed in the temp register. next, when the cpu writes the low byte tcnt1l, this byte of data is com- bined with the byte data in the temp regist er and all 16 bits are written to the tcnt1 timer/counter1 register simultaneously. consequently, the high byte tcnt1h must be accessed first for a full 16-bit register write operation.  tcnt1 timer/counter1 read when the cpu reads the low byte tcnt1l, the data of the low byte tcnt1l is sent to the cpu and the data of the high byte tcnt1h is placed in the temp register. when the cpu reads the data in the high byte tcnt1h, the cpu receives the data in the temp register. consequently, the low byte tcnt1l must be accessed first for a full 16-bit register read operation. table 16. clock 1 prescale select cs12 cs11 cs10 description 0 0 0 stop, the timer/counter1 is stopped. 001ck 010ck/8 011ck/64 100ck/256 101ck/1024 1 1 0 external pin t1, falling edge 1 1 1 external pin t1, rising edge bit 151413121110 9 8 $2d ($4d) msb tcnt1h $2c ($4c) lsb tcnt1l 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000
54 atmega161(l) 1228d?avr?02/07 the timer/counter1 is realized as an up or up/down (in pwm mode) counter with read and write access. if timer/counter1 is written to and a clock source is selected, the timer/counter1 continues countin g in the timer clock cycle after it is preset with the writ- ten value. timer/counter1 output compare register ? ocr1ah and ocr1al timer/counter1 output compare register ? ocr1bh and ocr1bl the output compare registers are 16-bit read/write registers. the timer/counter1 output compare registers contain the data to be continuously compared with timer/counter1. actions on compare matches are specified in the timer/counter1 control and status registers. a software write to the timer/counter register blocks compare matches in the nex t timer/counter clock cycle. this prevents immediate interrupts when in itializing the timer/counter. a compare match will set the compare interr upt flag in the cpu clock cycle following the compare event. since the output compare registers (ocr1a and ocr1b) are 16-bit registers, a tem- porary register (temp) is used when ocr1a/b are written to ensure that both bytes are updated simultaneously. when the cpu writes the high byte, ocr1ah or ocr1bh, the data is temporarily stored in the temp register. when the cpu writes the low byte, ocr1al or ocr1bl, the temp register is simultaneously written to ocr1ah or ocr1bh. consequently, the high byte ocr1ah or ocr1bh must be written first for a full 16-bit register write operation. the temp register is also used when accessing tcnt1 and icr1. if the main program and interrupt routines perform access to registers using temp, interrupts must be dis- abled during access from the main program and interrupt routines. timer/counter1 input capture register ? icr1h and icr1l bit 151413121110 9 8 $2b ($4b) msb ocr1ah $2a ($4a) lsb ocr1al 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 00000000 bit 151413121110 9 8 $29 ($49) msb ocr1bh $28 ($48) lsb ocr1bl 76543210 read/writer/wr/wr/wr/wr/wr/wr/wr/w r/wr/wr/wr/wr/wr/wr/wr/w initial value 0 0 0 0 0 0 0 0 00000000 bit 151413121110 9 8 $25 ($45) msb icr1h $24 ($44) lsb icr1l 76543210 read/writerrrrrrrr rrrrrrrr initial value00000000 00000000
55 atmega161(l) 1228d?avr?02/07 the input capture register is a 16-bit read-only register. when the rising or falling edge (according to the input capture edge setting, ices1) of the signal at the input capture pin (icp) is detected, the current value of the timer/counter1 register (tcnt1) is transferred to the input capture register (icr1). in the same cycle, the input capture flag (icf1) is set (one). since the input capture register (icr1) is a 16-bit register, a temporary register (temp) is used when icr1 is read to ensure that both bytes are read simultaneously. when the cpu reads the low byte icr1l, the data is sent to the cpu and the data of the high byte icr1h is placed in the te mp register. when the cpu reads the data in the high byte icr1h, the cpu receives t he data in the temp register. consequently, the low byte icr1l must be accessed first for a full 16-bit register read operation. the temp register is also used when accessing tcnt1, ocr1a and ocr1b. if the main program and interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routine. timer/counter1 in pwm mode when the pwm mode is selected, timer/counter1 and the output compare register1a (ocr1a) and the output compare register1b (ocr1b) form a dual 8-, 9-, or 10-bit, free-running, glitch-free, and phase-correct pwm with outputs on the pd5(oc1a) and pe2(oc1b) pins. in this mode the timer/counter1 acts as an up/down counter, count- ing up from $0000 to top (see table 17), where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the 8, 9, or 10 least significant bits (depends of the resolution) of ocr1a or ocr1b, the pd5(oc1a)/pe2(oc1b) pins are set or cleared according to the settings of the com1a1/com1a0 or com1b1/com1b0 bits in the timer/counter1 control register (tccr1a). refer to table 18 for details. alternatively, the timer/counter1 can be configured to a pwm that operates at twice the speed as in the mode described above. then the timer/counter1 and the output com- pare register1a (ocr1a) and the output compare register1b (ocr1b) form a dual 8-, 9- or 10-bit, free-running and glitch-free pwm with outputs on the pd5(oc1a) and pe2(oc1b) pins. as shown in table 17, the pwm operates at either 8-, 9-, or 10-bit resolution. note the unused bits in ocr1a, ocr1b and tcnt1 will automatically be writ ten to zero by hard- ware, i.e., bits 9 to 15 will be set to zero in ocr1a, ocr1b and tcnt1 if the 9-bit pwm resolution is selected. this makes it possible for the user to perform read-modify-write operations in any of the three resolution modes and the unused bits will be treated as don?t care. table 17. timer top values and pwm frequency ctc1 pwm11 pwm10 pwm resoluti on timer top value frequency 0 0 1 8-bit $00ff (255) f tck1 /510 0 1 0 9-bit $01ff (511) f tck1 /1022 0 1 1 10-bit $03ff (1023) f tck1 /2046 1 0 1 8-bit $00ff (255) f tck1 /256 1 1 0 9-bit $01ff (511) f tck1 /512 1 1 1 10-bit $03ff (1023) f tck1 /1024
56 atmega161(l) 1228d?avr?02/07 note: 1. x = a or b note that in the pwm mode, the 8, 9 or 10 least significant ocr1a/ocr1b bits (depends of resolution), when written, are transferred to a temporary location. they are latched when timer/counter1 reaches the val ue top. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr1a/ocr1b write. see figure 37 and figure 38 for an example in each mode. figure 37. effects on unsynchronized ocr1 latching (1) note: 1. note: x = a or b table 18. compare1 mode select in pwm mode (1) ctc1 com1x1 com1x0 effect on ocx1 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow. 1 1 1 set on compare match, cleared on overflow. counter v alue compare v alue pwm output oc1x synchronized ocr1x latch counter value compare value pwm output oc1x unsynchronized ocr1x latch glitch compare value changes compare v alue changes
57 atmega161(l) 1228d?avr?02/07 figure 38. effects of unsynchronized ocr1 latching in overflow mode 1 note: 1. note: x = a or b during the time between the write and the latch operation, a read from ocr1a or ocr1b will read the contents of the temporary location. this means that the most recently written value always will read out of ocr1a/b. when the ocr1x contains $0000 or top, and the up/down pwm mode is selected, the output oc1a/oc1b is updated to low or high on the next compare match according to the settings of com1a1/com1a0 or com1b1/c om1b0. this is shown in table 19. in overflow pwm mode, the output oc1a/oc1b is held low or high only when the output compare register contains top. note: 1. x = a or b in overflow pwm mode, the table above is only valid for ocr1x = top. in up/down pwm mode, the timer overflow flag1 (tov1) is set when the counter advances from $0000. in overflow pwm mode, the timer overflow flag is set as in nor- mal timer/counter mode. timer overflow interrupt1 operates exactly as in normal timer/counter mode, i.e., it is executed when tov1 is set, provided that timer overflow interrupt1 and global interrupts are enabled. this also applies to the timer output compare1 flags and interrupts. table 19. pwm outputs ocr1x = $0000 or top (1) com1x1 com1x0 ocr1x output oc1x 1 0 $0000 l 10top h 1 1 $0000 h 11top l pwm output oc1x pwm output oc1x unsynchronized oc1x latch synchronized oc1x latch
58 atmega161(l) 1228d?avr?02/07 watchdog timer the watchdog timer is clocked from a separate on-chip oscillator that runs at 1 mhz. this is the typical value at v cc = 5v. see characterization data for typical values at other v cc levels. by controlling the watchdog timer prescaler, the watchdog reset interval can be adjusted (see table 20 for a detail ed description). the wdr (watchdog reset) instruction resets the watchdog timer. eight different clock cycle periods can be selected to determine the reset period. if the reset period expires without another watchdog reset, the atmega161 resets and executes from the reset vector. for tim- ing details on the watchdog reset, refer to page 28. to prevent unintentional disabling of the watchdog, a special turn-off sequence must be followed when the watchdog is disabled. refer to the description of the watchdog timer control register for details. figure 39. watchdog timer watchdog timer control register ? wdtcr  bits 7..5 ? res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero. bit 4 ? wdtoe: watchdog turn-off enable this bit must be set (one) when the wde bit is cleared. otherwise, the watchdog will not be disabled. once set, hardware will clear this bit to zero after four clock cycles. refer to the description of the wde bit for a watchdog disable procedure. bit 3 ? wde: watchdog enable when the wde is set (one), the watchdog timer is enabled and if the wde is cleared (zero), the watchdog timer function is disabled. wde can only be cleared if the bit 76543210 $21 ($41) ? ? ? wdtoe wde wdp2 wdp1 wdp0 wdtcr read/write r r r r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
59 atmega161(l) 1228d?avr?02/07 wdtoe bit is set (one). to disable an enabled watchdog timer, the following proce- dure must be followed: 1. in the same operation, write a logical ?1? to wdtoe and wde. a logical ?1? must be written to wde even though it is set to one before the disable operation starts. 2. within the next four clock cycles, write a logical ?0? to wde. this disables the watchdog.  bits 2..0 ? wdp2, wdp1, wdp0: watchdog timer prescaler 2, 1 and 0 the wdp2, wdp1 and wdp0 bits determine the watchdog timer prescaling when the watchdog timer is enabled. the different prescaling values and their corresponding time-out periods are shown in table 20. note: 1. the frequency of the watchdog oscillator is voltage-dependent, as shown in the electrical characte ristics section. the wdr (watchdog reset) instruction should always be executed before the watchdog timer is enabled. this ensures th at the reset period will be in accordance with the watchdog timer prescale settings . if the watchdog timer is enabled without reset, the watchdog timer may not start counting from zero. to avoid unintentional mcu reset, the watchdog timer should be disabled or reset before changing the watchdog timer prescale select. table 20. watchdog timer prescale select (1) wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 3.0v typical time-out at v cc = 5.0v 0 0 0 16k 47 ms 15 ms 0 0 1 32k 94 ms 30 ms 0 1 0 64k 0.19 s 60 ms 0 1 1 128k 0.38 s 0.12 s 1 0 0 256k 0.75 s 0.24 s 1 0 1 512k 1.5 s 0.49 s 1 1 0 1,024k 3.0 s 0.97 s 1 1 1 2,048k 6.0 s 1.9 s
60 atmega161(l) 1228d?avr?02/07 eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access time is in the range of 1.9 - 3.4 ms, depending on the frequency of the rc oscillator used to time the eeprom access time. see table 22 for details. a self- timing function, however, lets the user software detect when the next byte can be writ- ten. if the user code contains code that writes the eeprom, some precaution must be taken. in heavily filtered power supplies, v cc is likely to rise or fall slowly on power- up/down. this causes the device for some period of time to run at a voltage lower than specified as minimum for the clock frequency used. cpu operation under these condi- tions is likely cause the program counter to perform unintentional jumps and eventually execute the eeprom write code. to secure eeprom integrit y, the user is advised to use an external under-voltage reset circuit in this case. in order to prevent unintentional eeprom writes, a specific write procedure must be fol- lowed. refer to the descripti on of the eeprom control regi ster for details on this. when the eeprom is written, the cpu is ha lted for two clock cycles before the next instruction is executed. when the eeprom is read, the cpu is halted for four clock cycles before the next instruction is executed. eeprom address register ? eearh and eearl  bits 15..9 ? res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero.  bits 8..0 ? eear8..0: eeprom address the eeprom address registers (eearh and eearl) specify the eeprom address in the 512-byte eeprom space. the eeprom data bytes are addressed linearly between 0 and 511. the initial value of eear is undefined. a proper value must be writ- ten before the eeprom may be accessed. eeprom data register ? eedr  bits 7..0 ? eedr7..0: eeprom data for the eeprom write operation, the eedr register contains the data to be written to the eeprom in the address gi ven by the eear register. for the eeprom read oper- ation, the eedr contains the data read out from the eeprom at the address given by eear. bit 151413121110 9 8 $1f ($3f) ???????eear8eearh $1e ($3e) eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 eearl 76543210 read/writerrrrrrrr/w r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 x xxxxxxxx bit 76543210 $1d ($3d) msb lsb eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
61 atmega161(l) 1228d?avr?02/07 eeprom control register ? eecr  bits 7..4 ? res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero. bit 3 ? eerie: eeprom ready interrupt enable when the i-bit in sreg and eerie are set (one), the eeprom ready interrupt is enabled. when cleared (zero), the interrupt is disabled. the eeprom ready interrupt generates a constant interrupt when eewe is cleared (zero). bit 2 ? eemwe: eeprom master write enable the eemwe bit determines w hether setting eewe to one causes the eeprom to be written. when eemwe is set (one), setting eewe will write data to the eeprom at the selected address. if eemwe is zero, setti ng eewe will have no effect. when eemwe has been set (one) by so ftware, hardware clears the bit to zero after four clock cycles. see the description of the eewe bit for an eeprom write procedure. bit 1 ? eewe: eeprom write enable the eeprom write enable si gnal (eewe) is the write strobe to the eeprom. when address and data are correctly set up, the eewe bit must be set to write the value into the eeprom. the eemwe bit must be set when the logical ?1? is written to eewe, oth- erwise no eeprom write takes place. t he following procedure should be followed when writing the eeprom (the order of steps 2 and 3 is not essential): 1. wait until eewe becomes zero. 2. write new eeprom addr ess to eear (optional). 3. write new eeprom data to eedr (optional). 4. write a logical ?1? to the eemwe bit in ee cr (to be able to write a logical ?1? to the eemwe bit, the eewe bit must be written to zero in the same cycle). 5. within four clock cycles after setting eemwe, write a logical ?1? to eewe. caution: an interrupt be tween step 4 and step 5 will make the write cycle fail, since the eeprom master write enable will time-out. if an interrupt routine accessing the eeprom is interrupting an other eeprom access , the eear or eedr register will be modified, causing the interrupted eeprom ac cess to fail. it is recommended to have the global interrupt flag cleared during the four last steps to avoid these problems. when the write access time has elapsed, the eewe bit is cleared (zero) by hardware. the user software can poll this bit and wait for a zero before writing the next byte. when eewe has been set, the cpu is halted for tw o cycles before the next instruction is executed. bit 0 ? eere: eeprom read enable the eeprom read enable sign al (eere) is the read strobe to the eeprom. when the correct address is set up in the eear register, the eere bit must be set. when the eere bit is cleared (zero) by hardware, requested data is found in the eedr register. the eeprom read access takes one instruction and there is no need to poll the eere bit 76543210 $1c ($3c) ? ? ? ? eerie eemwe eewe eere eecr read/write r r r r r/w r/w r/w r/w initial value000000x0
62 atmega161(l) 1228d?avr?02/07 bit. when eere has been set, the cpu is halted for four cycles before the next instruc- tion is executed. the user should poll the eewe bit before starting the read operation. if a write operation is in progress when new data or address is written to the eeprom i/o registers, the write operation will be in terrupted and the re sult is undefined. an rc oscillator is used to time eeprom write access. the table below lists the typical programming time listed for eeprom access from cpu. note: 1. see ?typical characteristics? on page 138 to find rc oscillator frequency. prevent eeprom corruption during periods of low v cc , the eeprom data can be corrupte d because the supply volt- age is too low for the cpu and the eeprom to operate prope rly. these issues are the same as for board-level systems using the eeprom, and the same design solutions should be applied. an eeprom data corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instructions incorrectly if the supply voltage for executin g instructions is too low. eeprom data corruption can easily be avoi ded by following thes e design recommen- dations (one is sufficient): 1. keep the avr reset active (low) during periods of insufficient power supply voltage. an external low v cc reset protection circuit can be applied. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will prevent the cpu from attempting to de code and execute instructions, effec- tively protecting the eeprom registers from unintentional writes. 3. store constants in flash memory if the ability to change memory contents from software is not required. flash memory cannot be updated by the cpu unless the boot loader software supports writing to the flash and the boot lock bits are configured so that writing to the flas h memory from the cpu is allowed. see ?boot loader support? on page 110 for details. table 21. eeprom access time from cpu?see ?t ypical characteristics? on page 138 to find rc oscillator frequency.? on page 62 (1) symbol no. of rc oscillator cycles min programming time max programming time eeprom write (from cpu) 2048 2.0 ms 3.4 ms
63 atmega161(l) 1228d?avr?02/07 serial peripheral interface ? spi the serial peripheral interface (spi) allows high-speed synchronous data transfer between the atmega161 and peripheral devices or between several avr devices. the atmega161 spi features include the following:  full-duplex, 3-wire synchronous data transfer  master or slave operation  lsb first or msb first data transfer  seven programmable bit rates  end-of-transmission interrupt flag  write collision flag protection  wake-up from idle mode (slave mode only)  double-speed (ck/2) master spi mode figure 40. spi block diagram spi2x spi2x divider /2/4/8/16/32/64/128
64 atmega161(l) 1228d?avr?02/07 the interconnection between master and slav e cpus with spi is shown in figure 41. the pb7(sck) pin is the clock output in the master mode and is the clock input in the slave mode. writing to the spi data register of the master cpu starts the spi clock generator, and the data written shifts out of the pb5(mosi) pin and into the pb5(mosi) pin of the slave cpu. after shifting one byte, the spi clock generator stops, setting the end-of-transmission flag (spif). if the spi interrupt enable bit (spie) in the spcr register is set, an interrupt is requested. the slave select input, pb4(ss ), is set low to select an individual slave spi device. the two shift registers in the master and the slave can be considered as one distributed 16-bi t circular shift register. this is shown in figure 41. when data is shifted from the master to the slave, data is also shifted in the opposite direction, simultaneously. this means that during one shift cycle, data in the master and the slave are interchanged. figure 41. spi master-slave interconnection the system is single-buffered in the transmit direction and double-buffered in the receive direction. this means that bytes to be transmitted cannot be written to the spi data register before the entire shift cycle is completed. when receiving data, however, a received byte must be read from the spi data register before the next byte has been completely shifted in. otherwise, the first byte is lost. when the spi is enabled, the data direction of the mosi, miso, sck and ss pins is overridden according to table 22. note: 1. see ?alternate functions of port b? on page 92 for a detailed description of how to define the direction of the user defined spi pins. table 22. spi pin overrides (1) pin direction, master spi direction, slave spi mosi user defined input miso input user defined sck user defined input ss user defined input
65 atmega161(l) 1228d?avr?02/07 ss pin functionality when the spi is configured as a master (mstr in spcr is set), the user can determine the direction of the ss pin. if ss is configured as an output, the pin is a general output pin, which does not affect the spi system. if ss is configured as an input, it must be held high to ensure master spi operation. if the ss pin is driven low by peripheral circuitry when the spi is configured as master with the ss pin defined as an input, the spi sys- tem interprets this as another master selecting the spi as a slave and starting to send data to it. to avoid bus contention, the spi system takes the following actions: 1. the mstr bit in spcr is cleared and the spi system becomes a slave. as a result of the spi becoming a slave, the mosi and sck pins become inputs. 2. the spif flag in spsr is set, and if the spi interrupt is enab led and the i-bit in sreg is set, the interrup t routine will be executed. thus, when interrupt-driven spi transmission is used in master mode, and there exists a possibility that ss is driven low. the in terrupt should always check that the mstr bit is still set. once the mstr bit has been cleared by a slave select, it must be set by the user to re-enable spi master mode. when the spi is configured as a slave, the ss pin is always input. when ss is held low, the spi is activated and miso becomes an output if configured so by the user. all other pins are inputs. when ss is driven high, all pins are inputs and the spi is passive, which means that it will not receive incoming data. note that the spi logic will be reset once the ss pin is brought high. if the ss pin is brought high during a transmission, the spi will stop sending and receiving immediately and both data received and data sent must be considered lost. data modes there are four combinations of sck phase and polarity with respect to serial data, which are determined by control bits cpha and cpol. the spi data transfer formats are shown in figure 42 and figure 43. figure 42. spi transfer format with cpha = 0 and dord = 0
66 atmega161(l) 1228d?avr?02/07 figure 43. spi transfer format with cpha = 1 and dord = 0 spi control register ? spcr bit 7 ? spie: spi interrupt enable this bit causes the spi interrupt to be execut ed if spif bit in the spsr register is set and if the global interrupt enable bit in sreg is set. bit 6 ? spe: spi enable when the spe bit is set (one), the spi is enabled. this bit mu st be set to enable any spi operations. bit 5 ? dord: data order when the dord bit is set (one), the lsb of the data word is transmitted first. when the dord bit is cleared (zero), the msb of the data word is transmitted first. bit 4 ? mstr: master/slave select this bit selects master spi mode when set (one), and slave spi mode when cleared (zero). if ss is configured as an input and is dr iven low while mstr is set, mstr will be cleared and spif in spsr will beco me set. the user will then have to set mstr to re- enable spi master mode. bit 3 ? cpol: clock polarity when this bit is set (one), sck is high when idle. when cpol is cleared (zero), sck is low when idle. refer to figure 42 and figure 43 for additional information. bit 2 ? cpha: clock phase refer to figure 42 or figure 43 for the functionality of this bit. bit 76543210 $0d ($2d) spie spe dord mstr cpol cpha spr1 spr0 spcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
67 atmega161(l) 1228d?avr?02/07  bits 1, 0 ? spr1, spr0: spi clock rate select 1 and 0 these two bits control the sck rate of the device configured as a master. spr1 and spr0 have no effect on the slave. the relationship between sck and the oscillator clock frequency (f cl ) is shown in table 23: note: 1. when the spi is configured as slave, the spi is only guaranteed to work at f cl /4. spi status register ? spsr bit 7 ? spif: spi interrupt flag when a serial transfer is complete, the spif bit is set (one) and an interrupt is gener- ated if spie in spcr is set (one) and global interrupts are enabled. if ss is an input and is driven low when the spi is in master mode, this will also set the spif flag. spif is cleared by hardware when executing the co rresponding interrupt handling vector. alter- natively, the spif bit is cleared by first reading the spi status register with spif set (one), then by accessing the spi data register (spdr). bit 6 ? wcol: write collision flag the wcol bit is set if the spi data regist er (spdr) is written during a data transfer. the wcol bit (and the spif bit) are cleared (zero) by first reading the spi status reg- ister with wcol set (one), and then by accessing the spi data register.  bits 5..1 ? res: reserved bits these bits are reserved bits in the atmega161 and will always read as zero. bit 0 ? spi2x: double spi speed bit when this bit is se t (one), the spi speed (sck frequency) will be doubled when the spi is in master mode (s ee table 23). this means that the maximum sck period will be two cpu clock periods. when the spi is configur ed as slave, the spi is only guaranteed to work at f cl /4. the spi interface on the atmega161 is also used for program memory and eeprom downloading or uploading. see page 125 for serial programming and verification. table 23. relationship between sck an d the oscillator frequency (1) spi2x spr1 spr0 sck frequency 000 f cl /4 001 f cl /16 010 f cl /64 011 f cl /128 100 f cl /2 101 f cl /8 110 f cl /32 111 f cl /64 bit 76543210 $0e ($2e) spifwcol?????spi2xspsr read/writerrrrrrrr/w initial value00000000
68 atmega161(l) 1228d?avr?02/07 spi data register ? spdr the spi data register is a read/write register used for data transfer between the regis- ter file and the spi shift register. writing to the register initiates data transmission. reading the register causes the shift register receive buffer to be read. bit 76543210 $0f ($2f) msb lsb spdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial valuexxxxxxxxu ndefined
69 atmega161(l) 1228d?avr?02/07 uarts the atmega161 features two full-duplex (separate receive and transmit registers) universal asynchronous receiver and transmitters (uarts). the main features are:  baud rate generator generates any baud rate  high baud rates at low xtal frequencies  8 or 9 bits data  noise filtering  overrun detection  framing error detection  false start bit detection  three separate interrupts on tx complete, tx data register empty, and rx complete  multi-processor communication mode  double-speed uart mode data transmission a block schematic of the uart transmitter is shown in figure 44. the two uarts are identical and the functionality is described in general for the two uarts. figure 44. uart transmitter data transmission is initiated by writing the data to be transmitted to the uart i/o data register, udrn. data is transferred from udrn to the transmit shift register when:  a new character has been written to udrn after the stop bit from the previous character has been shifted out. the shift register is loaded immediately. pd1/ pb3 u2xn 10(11)-bit tx shift register pin control logic uart control and status register (ucsrna) uart control and status register (ucsrnb) control logic baud rate generator txcn irq udren irq data b u s rxcien txcien udrien txcn udren rxenn txenn chr9n rxb8n txb8n rxcn txcn udren fen mpcmpn orn idle baud store udrn shift enable data b u s baud x 16 /16 uart i/o data register (udrn) xtal txdn n = 0,1
70 atmega161(l) 1228d?avr?02/07  a new character has been written to udrn before the stop bit from the previous character has been shifted out. the shift register is loaded when the stop bit of the character currently being transmitted has been shifted out. if the 10(11)-bit transmit shift register is empty, data is transferred from udrn to the shift register. at this time the udren (uar t data register empty) bit in the uart control and status register, ucsrna, is se t. when this bit is set (one), the uart is ready to receive the next character. at the same time as the data is transferred from udrn to the 10(11)-bit shift register, bit 0 of the shift register is cleared (start bit) and bit 9 or 10 is set (stop bit). if 9-bit data word is selected (the chr9n bit in the uart control and status register, ucsrnb, is set) , the txb8 bit in ucsrnb is transferred to bit 9 in the transmit shift register. on the baud rate clock following the transfer operation to the shift register, the start bit is shifted out on the txdn pin. then follows the data, lsb first. when the stop bit has been shifted out, the shift register is loaded if any new data has been written to the udrn during the transmission. during loading, udren is set. if there is no new data in the udrn register to send when the stop bit is shifted out, the udren flag will remain set until udrn is written again. when no new data has been written and the stop bit has been present on txdn for one bit length, the tx complete flag (txcn) in ucsrna is set. the txenn bit in ucsrnb enables the uart tr ansmitter when set (one). when this bit is cleared (zero), the pd1 (uart0) or pb3 (uart1) pin can be used for general i/o. when txenn is set, the uart transmitter will be connected to pd1 (uart0) or pb3 (uart1), which is forced to be an output pin regardless of the setting of the ddd1 bit in ddrd (uart0) or ddb3 in ddrb (uart1). no te that pb3 (uart1) also is used as one of the input pins to the analog comparator. it is therefore not recommended to use uart1 if the analog comparator is also used in the application at the same time.
71 atmega161(l) 1228d?avr?02/07 data reception figure 45 shows a block diagram of the uart receiver. figure 45. uart receiver the receiver front-end logic samples the signal on the rxdn pin at a frequency 16 times the baud rate. while the line is idle, one single sample of logical ?0? will be inter- preted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sample 1 denote the first zero-sample. following the 1-to-0 transition, the receiver samples the rxdn pin at samples 8, 9 and 10. if two or more of these three samples are found to be logical ?1?s, the start bit is rejected as a noise spike and the receiver starts looking for the next 1-to-0 transition. if, however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 8, 9 and 10. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 46. note that the description above is not valid when the uart trans- mission speed is doubled. s ee ?double-speed transmission? on page 78 for a detailed description. pd0/ pb2 data b u s baud uart i/o data register (udrn) 10(11)-bit rx shift register store udrn /16 baud x 16 rxenn txenn chr9n rxb8n txb8n u2xn rxcn txcn udren fen mpcmpn orn uart control and status register (ucsrnb) uart control and status register (ucsrna) rxcien txcien udrien txcn data b u s pin control logic baud rate generator xtal n = 0,1 rxcn irq data recovery logic rxdn
72 atmega161(l) 1228d?avr?02/07 figure 46. sampling received data (1) note: 1. this figure is not valid when the uart speed is doubled. see ?double-speed transmission? on page 78 for a detailed description. when the stop bit enters the receiver, the majority of the three samples must be one to accept the stop bit. if two or more samples are logical ?0?s, the framing error (fen) flag in the uart control and status register (ucsrna) is set. be fore reading the udrn register, the user should always check the fen bit to detect framing errors. whether or not a valid stop bit is detected at the end of a character reception cycle, the data is transferred to udrn and the rxcn flag in ucsrna is set. udrn is in fact two physically separate registers, one for transmitted data and one for received data. when udrn is read, the receive data register is accessed, and when udrn is written, the transmit data register is accessed. if 9-bit data word is selected (the chr9n bit in the uart control and status register [ucsrn b] is set), the rxb8n bit in ucsrnb is loaded with bit 9 in the transmit shift re gister when data is transferred to udrn. if, after having received a character, the ud rn register has not been read since the last receive, the overrun (orn) flag in ucsrnb is set. this means that the last data byte shifted into the shift register could not be transferred to udrn and has been lost. the orn bit is buffered and is updated when the valid data byte in udrn is read. thus, the user should always check the orn bit after reading the udrn register in order to detect any overruns if the baud rate is high or cpu load is high. when the rxen bit in the ucsrnb register is cleared (zero), the receiver is disabled. this means that the pd0 pin can be used as a general i/o pin. when rxen is set, the uart receiver will be connected to pd0 (uar t0) or pb2 (uart1), which is forced to be an input pin regardless of the setting of the ddd0 in ddrd (uart0) or ddb2 bit in ddrb (uart1). when pd0 (uart0) or pb2 (u art1) is forced to input by the uart, the portd0 (uart0) or portb2 (uart1) bit can still be used to control the pull-up resistor on the pin. note that pb2 (uart1) also is used as one of the input pins to the analog comparator. it is therefore not recommended to use uart 1 if the analog comparator also is used in the application at the same time. when the chr9n bit in the ucsrn b register is set, transmitted and received charac- ters are nine bits long plus start and stop bits. the ninth data bit to be transmitted is the txb8n bit in ucsrnb register. this bit must be set to the wanted value before a trans- mission is initiated by writing to the udrn register. the ninth data bit received is the rxb8n bit in the ucsrnb register.
73 atmega161(l) 1228d?avr?02/07 multi-processor communication mode the multi-processor communication mode enables several slave mcus to receive data from a master mcu. this is done by first decoding an address byte to find out which mcu has been addressed. if a particular slave mcu has been addressed, it will receive the following data bytes as normal, while the other slave mcus will ignore the data bytes until another address byte is received. for an mcu to act as a master mcu, it should enter 9-bit transmission mode (chr9n in ucsrnb set). the ninth bit must be one to i ndicate that an address byte is being trans- mitted, and zero to indicate that a data byte is being transmitted. for the slave mcus, the mechanism appears s lightly different for 8-bit and 9-bit recep- tion mode. in 8-bit reception mode (chr9n in ucsrnb cleared), the stop bit is one for an address byte and zero for a data byte. in 9-bit reception mode (chr9n in ucsrnb set), the ninth bit is one for an address byte and zero for a data byte, whereas the stop bit is always high. the following procedure should be used to exchange data in multi-processor communi- cation mode: 1. all slave mcus are in multi-proces sor communication mode (mpcmn in ucs- rna is set). 2. the master mcu sends an address byte and all slaves receive and read this byte. in the slave mcus, the rxcn flag in ucsrna will be set as normal. 3. each slave mcu reads the udrn regist er and determines if it has been selected. if so, it clears the mpcmn bit in ucsrna; otherwise, it waits for the next address byte. 4. for each received data byte, the rece iving mcu will set the receive complete flag (rxcn in ucsrna). in 8-bit mode , the receiving mcu will also generate a framing error (fen in ucsrna set), sinc e the stop bit is zero. the other slave mcus, which still have the mpcmn bit set, will ignore the data byte. in this case, the udrn register and the rxcn, fe n, or flags will not be affected. 5. after the last byte has been transferred, the process repeats from step 2. uart control uart0 i/o data register ? udr0 uart1 i/o data register ? udr1 the udrn register is actually two physica lly separate registers sharing the same i/o address. when writing to the register, the uart transmit data register is written. when reading from udrn, the uart receive data register is read. uart0 control and status registers ? ucsr0a bit 76543210 $0c ($2c) msb lsb udr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $03 ($23) msb lsb udr1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $0b ($2b) rxc0 txc0 udre0 fe0 or0 ? u2x0 mpcm0 ucsr0a read/write r r/w r r r r r/w r/w initial value00100000
74 atmega161(l) 1228d?avr?02/07 uart1 control and status registers ? ucsr1a bit 7 ? rxc0/rxc1: uart receive complete this bit is set (one) when a received character is transferred from the receiver shift register to udrn. the bit is set regardless of any detect ed framing errors. when the rxcien bit in ucsrnb is set, the uart receive complete interrupt will be executed when rxcn is set (one). rxcn is cleared by reading udrn. when interrupt-driven data reception is used, the uart receive comp lete interrupt routin e must read udrn in order to clear rxcn; otherwise, a new interrupt will occur once the interrupt routine terminates. bit 6 ? txc0/txc1: uart transmit complete this bit is set (one) when the entire charac ter (including the stop bit) in the transmit shift register has been shifted out and no new data has been written to udrn. this flag is especially useful in half-duplex communications interfaces, where a transmitting application must enter receive mode and free the communications bus immediately after completing the transmission. when the txcien bit in ucsrnb is set, setting of txcn causes the uart transmit complete interrupt to be executed. txcn is cleared by hardwar e when executing the corresponding interrupt handling vector. alter natively, the txcn bit is cleared (zero) by writing a logical ?1? to the bit. bit 5 ? udre0/udre1: uart data register empty this bit is set (one) when a character written to udrn is transferred to the transmit shift register. setting of this bit indicates that the transmitter is ready to receive a new char- acter for transmission. when the udrien bit in ucsrnb is set, t he uart transmit complete interrupt will be executed as long as udren is set and the gl obal interrupt enable bit in sreg is set. udren is cleared by writing udrn. when interrupt-driven data transmittal is used, the uart data register empty interrupt routine must write udrn in order to clear udren, otherwise a new in terrupt will occur once the inte rrupt routine terminates. udren is set (one) during reset to in dicate that the transmitter is ready. bit 4 ? fe0/fe1: framing error this bit is set if a framing error condition is detected, i.e., when the stop bit of an incom- ing character is zero. the fen bit is cleared when the stop bit of received data is one. bit 3 ? or0/or1: overrun this bit is set if an overrun condition is detected, i.e., when a character already present in the udrn register is not read before th e next character has b een shifted into the receiver shift register. the orn bit is buffe red, which means that it will be set once the valid data still in udrn is read. the orn bit is cleared (zero) when data is received and transferred to udrn. bit 76543210 $02 ($22) rxc1 txc1 udre1 fe1 or1 ? u2x1 mpcm1 ucsr1a read/writerr/wrrrrr/wr/w initial value00100000
75 atmega161(l) 1228d?avr?02/07 bit 2 ? res: reserved bit this bit is reserved bit in the at mega161 and will always read as zero. bit 1 ? u2x0/u2x1: double the uart transmission speed when this bit is set (one), the uart spee d will be doubled. this means that a bit will be transmitted/received in 8 cpu clock perio ds instead of 16 cpu clock periods. for a detailed description, see ?double-speed transmission? on page 78. bit 0 ? mpcm0/mpcm1: multi-processor communication mode this bit is used to enter multi-processor communication mode. the bit is set when the slave mcu waits for an address byte to be received. when the mcu has been addressed, the mcu switches off the mpcmn bit and starts data reception. for a detailed description, see ?multi-processor communication mode?. uart0 control and status registers ? ucsr0b uart1 control and status registers ? ucsr1b bit 7 ? rxcie0/rxcie1: rx complete interrupt enable when this bit is set (one), a setting of the rxcn bit in ucsrna will cause the receive complete interrupt routine to be executed, provided that global interrupts are enabled. bit 6 ? txcie0/txcie1: tx complete interrupt enable when this bit is set (one), a setting of the txcn bit in ucsrna will cause the transmit complete interrupt routine to be executed, provided that global interrupts are enabled. bit 5 ? udrie0/udrei1: uart data re gister empty interrupt enable when this bit is set (one), a setting of the udren bit in ucsrna will cause the uart data register empty interrupt routine to be executed, provided that global interrupts are enabled. bit 4 ? rxen0/rxen1: receiver enable this bit enables the uart receiver when set (one). when the receiver is disabled, the txcn, orn and fen status flags cannot become set. if these flags are set, turning off rxen does not cause them to be cleared. bit 3 ? txen0/txen1: transmitter enable this bit enables the uart transmitter when se t (one). when disabling the transmitter while transmitting a character, the transmitter is not disabled before the character in the shift register plus any follo wing character in udrn has been completely transmitted. bit 76543210 $0a ($2a) rxcie0 txcie0 udrie0 rxen0 txen0 chr90 rxb80 txb80 ucsr0b read/write r/w r/w r/w r/w r/w r/w r r/w initial value 0 0 0 0 0 0 1 0 bit 76543210 $01 ($21) rxcie1 txcie1 udrie1 rxen1 txen1 chr91 rxb81 txb81 ucsr1b read/write r/w r/w r/w r/w r/w r/w r r/w initial value00000010
76 atmega161(l) 1228d?avr?02/07 bit 2 ? chr90/chr91: 9- bit characters when this bit is set (one), transmitted and received characters are nine bits long, plus start and stop bits. the ninth bit is read and written by using the rxb8n and txb8 bits in ucsrnb, respectively. the ninth data bit can be used as an extra stop bit or a parity bit. bit 1 ? rxb80/rxb81: receive data bit 8 when chr9n is set (one), rxb8n is the ninth data bit of the received character. bit 0 ? txb80/txb81: transmit data bit 8 when chr9n is set (one), txb8n is the ninth data bit in the character to be transmitted. baud rate generator the baud rate generator is a frequency divider that generates baud rates according to the following equation:  baud = baud rate f ck = crystal clock frequency  ubr = contents of the ubr rh and ubrr registers (0 - 4095)  note that this equation is not valid when the uart transmission speed is doubled. see ?double-speed transmission? for a detailed description. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 24. ubr val ues that yield an actual baud rate differ- ing less than 2% from the target baud rate are boldface in the table. however, using baud rates that have more than 1% error is not recommended. high error ratings give less noise resistance. baud f ck 16(ubr 1 ) + --------------------------------- - =
77 atmega161(l) 1228d?avr?02/07 table 24. ubr settings at variou s crystal frequencies baud rate 1mhz %error 1.8432 mhz %error 2mhz %error 2.4576 mhz %error 2400 ubr= 25 0.2 ubr= 47 0.0 ubr= 51 0.2 ubr= 63 0.0 4800 ubr= 12 0.2 ubr= 23 0.0 ubr= 25 0.2 ubr= 31 0.0 9600 ubr= 6 7.5 ubr= 11 0.0 ubr= 12 0.2 ubr= 15 0.0 14400 ubr= 3 7.8 ubr= 70.0 ubr= 8 3.7 ubr= 10 3.1 19200 ubr= 2 7.8 ubr= 50.0 ubr= 6 7.5 ubr= 70.0 28800 ubr= 1 7.8 ubr= 30.0 ubr= 3 7.8 ubr= 4 6.3 38400 ubr= 1 22.9 ubr= 20.0 ubr= 2 7.8 ubr= 30.0 57600 ubr= 0 7.8 ubr= 10.0 ubr= 1 7.8 ubr= 2 12.5 76800 ubr= 0 22.9 ubr= 1 33.3 ubr= 1 22.9 ubr= 10.0 115200 ubr= 0 84.3 ubr= 00.0 ubr= 0 7.8 ubr= 0 25.0 baud rate 3.2768 mhz %error 3.6864 mhz %error 4mhz %error 4.608 mhz %error 2400 ubr= 84 0.4 ubr= 95 0.0 ubr= 103 0.2 ubr= 119 0.0 4800 ubr= 42 0.8 ubr= 47 0.0 ubr= 51 0.2 ubr= 59 0.0 9600 ubr= 20 1.6 ubr= 23 0.0 ubr= 25 0.2 ubr= 29 0.0 14400 ubr= 13 1.6 ubr= 15 0.0 ubr= 16 2.1 ubr= 19 0.0 19200 ubr= 10 3.1 ubr= 11 0.0 ubr= 12 0.2 ubr= 14 0.0 28800 ubr= 61.6 ubr= 70.0 ubr= 8 3.7 ubr= 90.0 38400 ubr= 4 6.3 ubr= 50.0 ubr= 6 7.5 ubr= 7 6.7 57600 ubr= 3 12.5 ubr= 30.0 ubr= 3 7.8 ubr= 40.0 76800 ubr= 2 12.5 ubr= 20.0 ubr= 2 7.8 ubr= 3 6.7 115200 ubr= 1 12.5 ubr= 10.0 ubr= 1 7.8 ubr= 2 20.0 baud rate 7.3728 mhz %error 8mhz %error 9.216 mhz %error 11.059 mhz %error 2400 ubr= 191 0.0 ubr= 207 0.2 ubr= 239 0.0 ubr= 287 - 4800 ubr= 95 0.0 ubr= 103 0.2 ubr= 119 0.0 ubr= 143 0.0 9600 ubr= 47 0.0 ubr= 51 0.2 ubr= 59 0.0 ubr= 71 0.0 14400 ubr= 31 0.0 ubr= 34 0.8 ubr= 39 0.0 ubr= 47 0.0 19200 ubr= 23 0.0 ubr= 25 0.2 ubr= 29 0.0 ubr= 35 0.0 28800 ubr= 15 0.0 ubr= 16 2.1 ubr= 19 0.0 ubr= 23 0.0 38400 ubr= 11 0.0 ubr= 12 0.2 ubr= 14 0.0 ubr= 17 0.0 57600 ubr= 70.0 ubr= 8 3.7 ubr= 90.0 ubr= 11 0.0 76800 ubr= 50.0 ubr= 6 7.5 ubr= 7 6.7 ubr= 80.0 115200 ubr= 30.0 ubr= 3 7.8 ubr= 40.0 ubr= 50.0
78 atmega161(l) 1228d?avr?02/07 uart0 and uart1 high byte baud rate register ubrrhi the uart baud register is a 12-bit register. the four most significant bits are located in a separate register, ubrrhi. note that both uart0 and uart1 share this register. bit 7 to bit 4 of ubrrhi contain the four most significant bits of the uart1 baud register. bit 3 to bit 0 contain the four most sign ificant bits of the uart0 baud register. uart0 baud rate register low byte ? ubrr0 uart1 baud rate register low byte ? ubrr1 ubrrn stores the eight least significant bits of the uart baud rate register. double-speed transmission the atmega161 provides a separate uart mode that allows the user to double the communication speed. by setting the u2x bit in uart control and status register ucsrna, the uart speed will be doubled. the data reception will differ slightly from normal mode. since the speed is doubled, the receiver front-end logic samples the sig- nals on rxdn pin at a frequency 8 times the baud rate. while the line is idle, one single sample of logical ?0? will be in terpreted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sample 1 denote the first zero-sample. following the 1-to-0 transition, the receiver samples the rxdn pin at samples 4, 5 and 6. if two or more of these three samples are found to be logical ?1?s, the start bit is rejected as a noise spike and the receiver starts looking for the next 1-to-0 transition. if, however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 4, 5 and 6. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 47. figure 47. sampling received data when th e transmission speed is doubled bit 76543210 $20 ($40) msb1 lsb1 msb0 lsb0 ubrrhi read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $09 ($29) msb lsb ubrr0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $00 ($20) msb lsb ubrr1 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 start bit d0 d1 d2 d3 d4 d5 d6 d7 stop bit rxd receiver sampling
79 atmega161(l) 1228d?avr?02/07 the baud rate generator in double uart speed mode note that the baud rate equation is different from the equation on page 76 when the uart speed is doubled:  baud = baud rate f ck = crystal clock frequency  ubr = contents of the ubrrhi and ubrr registers (0 - 4095)  note that this equation is only valid when the uart transmission speed is doubled. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 24. ubr val ues that yield an actual baud rate differ- ing less than 1.5% from the target baud rate are boldface in the table. however, since the number of samples are reduced and the system clock might have some variance (this applies especially when using resonators), it is recommended that the baud rate error be less than 0.5%. baud f ck 8(ubr 1 ) + ------------------------------ =
80 atmega161(l) 1228d?avr?02/07 table 25. ubr settings at various crystal frequencies in double-speed mode baud rate 1.0000 mhz % error 1.8432 mhz % error 2.0000 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 ubr = 51 ubr = 25 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 2 ubr = 1 ubr = 1 ubr = 0 - 0.2 0.2 0.2 3.7 7.5 7.8 7.8 7.8 22.9 84.3 - ubr = 95 ubr = 47 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 5 ubr = 3 ubr = 2 ubr = 1 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ubr = 103 ubr = 51 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 2 ubr = 1 ubr = 0 0.2 0.2 0.2 2.1 0.2 3.7 7.5 7.8 7.8 7.8 84.3 baud rate 3.2768 mhz % error 3.6864 mhz % error 4.0000 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 460800 912600 ubr = 170 ubr = 84 ubr = 42 ubr = 27 ubr = 20 ubr = 13 ubr = 10 ubr = 6 ubr = 4 ubr = 3 ubr = 1 ubr = 0 - 0.2 0.4 0.8 1.6 1.6 1.6 3.1 1.6 6.2 12.5 12.5 12.5 - ubr = 191 ubr = 95 ubr = 47 ubr = 31 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 5 ubr = 3 ubr = 1 ubr = 0 - 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 - ubr = 207 ubr = 103 ubr = 51 ubr = 34 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 6 ubr = 3 ubr = 1 ubr = 0 ubr = 0 0.2 0.2 0.2 0.8 0.2 2.1 0.2 3.7 7.5 7.8 7.8 7.8 84.3 baud rate 7.3728 mhz % error 8.0000 mhz % error 9.2160 mhz % error 2400 4800 9600 14400 19200 28800 38400 57600 76800 115200 230400 460800 912600 ubr = 383 ubr = 191 ubr = 95 ubr = 63 ubr = 47 ubr = 31 ubr = 23 ubr = 15 ubr = 11 ubr = 7 ubr = 3 ubr = 1 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ubr = 416 ubr = 207 ubr = 103 ubr = 68 ubr = 51 ubr = 34 ubr = 25 ubr = 16 ubr = 12 ubr = 8 ubr = 3 ubr = 1 ubr = 0 0.1 0.2 0.2 0.6 0.2 0.8 0.2 2.1 0.2 3.7 7.8 7.8 7.8 ubr = 479 ubr = 239 ubr = 119 ubr = 79 ubr = 59 ubr = 39 ubr = 29 ubr = 19 ubr = 14 ubr = 9 ubr = 4 ubr = 2 ubr = 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 20.0 20.0
81 atmega161(l) 1228d?avr?02/07 analog comparator the analog comparator compares the input values on the positive input pb2 (ain0) and negative input pb3 (ain1). when the voltage on the positive input pb2 (ain0) is higher than the voltage on the negative input pb3 (ain1), the analog comparator output (aco) is set (one). the comparator?s output can be set to trigger the timer/counter1 input capture function. in addition, the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select interrupt triggering on compar- ator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 48 . figure 48. analog comparator block diagram analog comparator control and status register ? acsr bit 7 ? acd: analog comparator disable when this bit is set (one), the power to the analog comparator is switched off. this bit can be set at any time to turn off the analog comparator. this will reduce power con- sumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. otherwise, an interrupt can occur when the bit is changed. bit 6 ? ainbg: analog comp arator bandgap select when this bit is set, a fixed bandgap voltag e of 1.22 0.05v replaces the normal input to the positive input (ain0) of the comparator. when this bit is cleared, the normal input pin pb2 is applied to the positive input of the comparator. bit 5 ? aco: analog comparator output aco is directly connected to the comparator output. bit 76543210 $08 ($28) acd ainbg aco aci acie acic acis1 acis0 acsr read/write r/w r/w r r/w r/w r/w r/w r/w initial value 0 0 n/a 0 0 0 0 0
82 atmega161(l) 1228d?avr?02/07 bit 4 ? aci: analog comparator interrupt flag this bit is set (one) when a comparator output event triggers the interrupt mode defined by aci1 and aci0. the analog comparator in terrupt routine is executed if the acie bit is set (one) and the i-bit in sreg is set (one ). aci is cleared by hardware when execut- ing the corresponding interrupt handling vector. alternatively, aci is cleared by writing a logical ?1? to the flag. bit 3 ? acie: analog comparator interrupt enable when the acie bit is set (one) and the i-bit in the status register is set (one), the ana- log comparator interrupt is enabled. when cleared (zero), the interrupt is disabled. bit 2 ? acic: analog comparator input capture enable when set (one), this bit enables the input capture function in timer/counter1 to be trig- gered by the analog comparator. the comparator output is, in this case, directly connected to the input capture front-end logic, making the comparator utilize the noise canceler and edge select features of the timer/counter1 input capture interrupt. when cleared (zero), no connection between the analog comparator and the input capture function is given. to make the comparator trigger the timer/counter1 input capture interrupt, the ticie1 bit in the timer interrupt mask register (timsk) must be set (one).  bits 1, 0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which comparator events trigger the analog comparator interrupt. the different settings are shown in table 26. note: 1. when changing the acis1/acis0 bits, the analog comparator interrupt must be dis- abled by clearing its interrupt enable bit in the acsr register. otherwise, an interrupt can occur when the bits are changed. caution: using the sbi or cbi instruction on bits other than aci in th is register will write a one back into aci if it is read as set, thus clearing the flag. the analog comparator pins (pb2 and pb3) are also used as the txd1 and rxd1 pins for uart1. note that if the uart1 transceiver or rece iver is enabled, the uart1 will override the settings in the ddrb register even if the analog comparator is enabled. therefore, it is not recommended to use uart1 if the analog comparator is needed in the same application at the same time. see ?uarts? on page 69 for more details. table 26. acis1/acis0 settings (1) acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle 01reserved 1 0 comparator interrupt on falling output edge 1 1 comparator interrupt on rising output edge
83 atmega161(l) 1228d?avr?02/07 internal voltage reference atmega161 features an internal voltage reference with a nominal voltage of 1.22v. this reference can be used as an input to the analog comparator. voltage reference enable signals and start- up time the voltage reference has a start-up time that may have an influence on the way it should be used. the maximum start-up time is tbd. to save power, the reference is on only when the ainbg bit in acsr is set. thus the user must always allow the reference to start up before the output from the analog comparator is used. the bandgap refer- ence uses approximately 10 a, and to reduce the power consumption in power-down mode, the user can turn off the reference when entering this mode.
84 atmega161(l) 1228d?avr?02/07 interface to external memory with all the features the external memory in terface provides, it is well suited to operate as an interface to memory devices such as external sram and flash, and peripherals such as lcd display, a/d, d/a, etc. the control bits for the external memory interface are located in two registers, the mcu control register (mcucr) and the extended mcu control register (emcucr). mcu control register ? mcucr extended mcu control register ? emcucr  bit 7 mcucr ? sre: external sram enable when the sre bit is set (one), the external memory interface is enabled and the pin functions ad0 - 7 (port a), a8 - 15 (port c), ale (port e), wr and rd (port d) are acti- vated as the alternate pin functions. the sr e bit overrides any pi n direction settings in the respective data direction registers. see figure 50 through figure 53 for a descrip- tion of the external memory pin functions. when the sre bit is cleared (zero), the external data memory interface is dis abled and the normal pin and data direction set- tings are used  bits 6..4 emcucr ? srl2, srl1, srl0: wait state page limit it is possible to configure different wait st ates for different external memory addresses. the external memory address space can be divided into two pages with different wait state bits. the srl2, srl1 and srl0 bits select the split of the pages (see table 28 and figure 49). as defaults, the srl2, srl1 and srl0 bits are set to zero and the entire external memory address space is treated as one page. when the entire sram address space is configured as one page, the wait states are configured by the srw11 and srw10 bits.  bit 1 emcucr and bit 6 mcucr ? srw11, srw10: wait state select bits for upper page the srw11 and srw10 bits control the number of wait states for the upper page of the external memory address space (see table 27). note that if the srl2, srl1, and srl0 bits are set to zero, the sr w11 and srw10 bit settings will define the wait state of the entire sram address space. bit 76543210 $35 ($55) sre srw10 se sm1 isc11 isc10 isc01 isc00 mcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $36 ($56) sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc20 emcucr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
85 atmega161(l) 1228d?avr?02/07  bits 3..2 emcucr ? srw01, srw00: wait state select bits for lower page the srw01 and srw00 bits control the number of wait states for the lower page of the external memory address space (see table 27). note: 1. n = 0 or 1 (lower/upper page) for further details of the timing and wait states of the external memory interface, see figure 50 through figure 53 for how the setting of the srw bits affects the timing. table 27. wait states (1) srwn1 srwn0 wait states 00no wait states 0 1 wait one cycle duri ng read/write strobe 1 0 wait two cycles during read/write strobe 1 1 wait two cycles during read/writ e and wait one cycl e before driving out new address table 28. page limits with different settings of srl2..0 srl2 srl1 srl0 page limits 000 lower page = n/a upper page = $0460-$ffff 001 lower page = $0460-$1fff upper page = $2000-$ffff 010 lower page = $0460-$4fff upper page = $4000-$ffff 011 lower page = $0460-$5fff upper page = $6000-$ffff 100 lower page = $0460-$7fff upper page = $8000-$ffff 101 lower page = $0460-$9fff upper page = $a000-$ffff 110 lower page = $0460-$bfff upper page = $c000-$ffff 111 lower page = $0460-$dfff upper page = $e000-$ffff
86 atmega161(l) 1228d?avr?02/07 figure 49. external memory with page select figure 50. external data memory cycles without wait state (srwn1 = 0 and srwn0 =0) (1) note: 1. srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page). the ale pulse in period t4 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t4 if ale is present (the next instruct ion accesses the ram). $0000 data memory $0460 external memory (0-63k x 8) $ffff internal memory srl[2..0] srw11 srw10 srw01 srw00 lower page upper page system clock ? ale wr rd data/address [7..0] data/address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t4 xx xx xx xx prev. addr. xx prev. data xx prev. data
87 atmega161(l) 1228d?avr?02/07 figure 51. external data memory cycles with srwn1 = 0 and srwn0 = 1 (1) note: 1. srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page). the ale pulse in period t5 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t5 if ale is present (the next instruction accesses the ram). figure 52. external data memory cycles with srwn1 = 1 and srwn0 = 0 (1) note: 1. srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page). the ale pulse in period t6 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t6 if ale is present (the next instruction accesses the ram). figure 53. external data memory cycles with srwn1 = 1 and srwn0 = 1 (1) note: 1. srwn1 = srw11 (upper page) or srw01 (lower page), srwn0 = srw10 (upper page) or srw00 (lower page). the ale pulse in period t7 is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t7 if ale is present (the next instruction accesses the ram). system clock ? ale wr rd data/address [7..0] data/address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t5 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 system clock ? ale wr rd data/address [7..0] data/address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t6 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 t5 system clock ? ale wr rd data/address [7..0] data/address [7..0] address [15..8] address address address t1 t2 t3 xx data data write read t7 xx xx xx xx prev. addr. xx prev. data xx prev. data t4 t5 t6
88 atmega161(l) 1228d?avr?02/07 using the external memory interface the interface consists of: port a: multiplexed low-order address bus and data bus port c: high-order address bus the ale pin: address latch enable the rd and wr pin: read and write strobes the external memory interface is enabled by setting the external sram enable bit (sre) of the mcu control register (mcucr) and will override the setting of the data direction registers ddra, ddrd and ddre. when the sre bit is cleared (zero), the external memory interface is disabled and th e normal pin and data direction settings are used. when sre is low, the address space above the internal sram boundary is not mapped into the internal sram, as in avr parts do not have an external memory interface. when ale goes from high to low, there is a valid address on port a. ale is low during a data transfer. rd and wr are active when accessing the external memory only. when the external memory interface is ena bled, the ale signal may have short pulses when accessing the internal ram, but the ale signal is stable when accessing the external memory. figure 54 sketches how to connect an external sram to the avr using eight latches that are transparent when g is high. figure 54. external sram connected to the avr for details on the timing for the sram interface, please see figure 83 through figure 86 and table 51 through table 58. d[7:0] a[7:0] a[15:8] rd wr sram dq g port a ale port c rd wr avr
89 atmega161(l) 1228d?avr?02/07 i/o ports all avr ports have true read-modify-write functionality when used as general digital i/o ports. this means that the direction of one port pin can be changed without unintention- ally changing the direction of any other pin with the sbi and cbi instructions. the same applies for changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input). port a port a is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port a, one each for the data register ? porta, $1b($3b), data direct ion register ? ddra, $1a($3a) and the port a input pins ? pina, $19($39). the port a input pins address is read-only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port a output buffers can sink 20 ma and thus drive led displays di rectly. when pins pa0 to pa7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resis- tors are activated. the port a pins have alternate functions related to the optional external memory inter- face. port a can be configured to be the multiplexed low-order address/data bus during accesses to the external data memory. in this mode, port a has internal pull-up resistors. when port a is set to the alternate function by the sre (external sram enable) bit in the mcucr (mcu control register), the alternate settings override the data direction register. port a data register ? porta port a data direction register ? ddra port a input pins address ? pina the port a input pins address (pina) is not a register; this address enables access to the physical value on each port a pin. when reading porta, the port a data latch is read and when reading pina, the logical values present on the pins are read. port a as general digital i/o all eight pins in port a have equal func tionality when used as digital i/o pins. pan, general i/o pin: the ddan bit in the ddra register selects the direction of this pin. if ddan is set (one), pan is configured as an output pin. if ddan is cleared (zero), pan is configured as an input pin. if portan is set (one) when the pin is configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the portan has to be cleared (zero) or the pin has to be configured as an output pin. the bit 76543210 $1b ($3b) porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 porta read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $1a ($3a) dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 ddra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $19 ($39) pina7 pina6 pina5 pina4 pi na3 pina2 pina1 pina0 pina read/writerrrrrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
90 atmega161(l) 1228d?avr?02/07 port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. note: 1. n: 7,6?0, pin number port a schematics note that all port pins are synchronized. the synchronization latch is, however, not shown in the figure. figure 55. port a schematic diagrams (pins pa0 - pa7) table 29. ddan effects on port a pins (1) ddan portan i/o pull-up comment 0 0 input no tri-state (high-z) 0 1 input yes pan will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
91 atmega161(l) 1228d?avr?02/07 port b port b is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port b, one each for the data register ? portb, $1 8($38), data direction register ? ddrb, $17($37) and the port b input pins ? pinb, $16($36). the port b input pins address is read-only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port b output buffers can sink 20 ma and thus drive led displays di rectly. when pins pb0 to pb7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resis- tors are activated. the port b pins with alternate functions are shown in table 30. note: 1. when the pins are used for the altern ate function, the ddrb and portb registers have to be set according to the alternate function description. port b data register ? portb port b data direction register ? ddrb port b input pins address ? pinb the port b input pins address (pinb) is not a register; this address enables access to the physical value on each port b pin. when reading portb, the port b data latch is read and when reading pinb, the logical values present on the pins are read. table 30. port b pin alternate functions (1) port pin alternate functions pb0 oc0 (timer/counter0 compare match ou tput)/t0 (timer/counter0 external counter input) pb1 oc2 (timer/counter2 compare match ou tput)/t1 (timer/counter1 external counter input) pb2 rxd1 (uart1 input line)/ain0 (a nalog comparator positive input) pb3 txd1 (uart1 output line)/ain1 (analog comparator negative input) pb4 ss (spi slave select input) pb5 mosi (spi bus master output/slave input) pb6 miso (spi bus master input/slave output) pb7 sck (spi bus serial clock) bit 76543210 $18 ($38) portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $17 ($37) ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $16 ($36) pinb7 pinb6 pinb5 pinb4 pinb 3 pinb2 pinb1 pinb0 pinb read/writerrrrrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
92 atmega161(l) 1228d?avr?02/07 port b as general digital i/o all eight pins in port b have equal func tionality when used as digital i/o pins. pbn, general i/o pin: the ddbn bit in the ddrb register selects the direction of this pin. if ddbn is set (one), pbn is configured as an output pin. if ddbn is cleared (zero), pbn is configured as an input pin. if portbn is set (one) when the pin is configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the portbn has to be cleared (zero) or the pin has to be configured as an output pin. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. note: 1. n: 7,6?0, pin number alternate functions of port b the alternate pin configuration is as follows: sck ? port b, bit 7 sck: master clock output, slave clock input pin for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb7. when the spi is enabled as a master, the da ta direction of this pin is controlled by ddb7. when the pin is forced to be an input, the pull-up can still be controlled by the portb7 bit. see the description of the spi port for further details. miso ? port b, bit 6 miso: master data input, slave data output pin for spi channel. when the spi is enabled as a master, this pin is configured as an input regardless of the setting of ddb6. when the spi is enabled as a slave, th e data direction of this pin is controlled by ddb6. when the pin is forced to be an input, the pull-up can still be controlled by the portb6 bit. see the description of the spi port for further details. mosi ? port b, bit 5 mosi: spi master data output, slave data input for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb5. when the spi is enabled as a master, the da ta direction of this pin is controlled by ddb5. when the pin is forced to be an input, the pull-up can still be controlled by the portb5 bit. see the description of the spi port for further details. ss ? port b, bit 4 ss : slave port select input. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb5 . as a slave, the spi is activated when this pin is driven low. when the spi is enabled as a master, the data direction of this pin is controlled by ddb5. when the pin is forced to be an input, the pull-up can still be con- trolled by the portb5 bit. see the description of the spi port for further details. table 31. ddbn effects on port b pins (1) ddbn portbn i/o pull-up comment 0 0 input no tri-state (high-z) 0 1 input yes pbn will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
93 atmega161(l) 1228d?avr?02/07  txd1/ain1 ? port b, bit 3 ain1, analog comparator negative input. this pin also serves as the negative input of the on-chip analog comparator. txd1, transmit data (data output pin for the uart1). when the uart1 transmitter is enabled, this pin is configured as an output regardless of the value of ddrb3.  rxd1/ain0 ? port b, bit 2 ain0, analog comparator positive input. this pin also serves as the positive input of the on-chip analog comparator. rxd1, receive data (data input pin for the uart1). when th e uart1 receiver is enabled, this pin is configured as an input regardless of the value of ddrb2. when the uart1 forces this pin to be an input, a logical ?1? in portb2 will turn on the internal pull-up. oc2/t1 ? port b, bit 1 t1: timer/counter1 counter source. see ?timer/counter1? on page 49 for further details. oc2, output compare match output: the pb1 pin can serve as an external output when the timer/counter2 compare matches. the pb1 pin has to be configured as an output (ddb1 set [one]) to serve this function. see ?8-bit timer/counters t/c0 and t/c2? on page 40 for further details. the oc2 pin is also the output pin for the pwm mode timer function. oc0/t0 ? port b, bit 0 t0: timer/counter0 counter source. see ?8-bit timer/counters t/c0 and t/c2? on page 40 further details. oc0, output compare match output: the pb0 pin can serve as an external output when the timer/counter0 compare matches. the pb0 pin has to be configured as an output (ddb0 set [one]) to serve this function. see ?8-bit timer/counters t/c0 and t/c2? on page 40 for further details and how to enable the output. the oc0 pin is also the output pin for the pwm mode timer function.
94 atmega161(l) 1228d?avr?02/07 port b schematics note that all port pins are synchronized. the synchronization latches are, however, not shown in the figures. figure 56. port b schematic diagram (pins pb0 and pb1) pbn ddbn portbn comp. match x comx0 comx1 wp: wd: rl: rp: rd: write portb write ddrb read portb latch read portb pin read ddrb focx pwmx n: 0,1 x: 0,2 csn2 csn1 csn0
95 atmega161(l) 1228d?avr?02/07 figure 57. port b schematic diagram (pin pb2) figure 58. port b schematic diagram (pin pb3) da ta bus d d q q reset reset c c wd wp rd mos pull- up pb2 rxd1 rxen1 wp: wd: rl: rp: rd: rxd1: rxen1: write portb write ddrb read portb latch read portb pin read ddrb uart1 receive data uart1 receive enable ddb2 portb2 rl rp ain0: analog comparator positive input ain0 da ta bus d d q q reset reset c c wd wp rd rp rl mos pull- up pb3 r r wp: wd: rl: rp: rd: txd1: txen1: write portb write ddrb read portb latch read portb pin read ddrb uart1 transmit data uart1 transmit enable ddb3 portb3 txen1 txd1 ain1 ain1: analog comparator negative input
96 atmega161(l) 1228d?avr?02/07 figure 59. port b schematic diagram (pin pb4) figure 60. port b schematic diagram (pin pb5) data bus d d q q reset reset c c wd wp rd mos pull- up pb4 spi ss mstr spe wp: wd: rl: rp: rd: mstr: spe: write portb write ddrb read portb latch read portb pin read ddrb spi master enable spi enable ddb4 portb4 rl rp data bus d d q q reset reset c c wd wp rd mos pull- up pb5 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb5 portb5 spe mstr spi master out spi slave in rl rp
97 atmega161(l) 1228d?avr?02/07 figure 61. port b schematic diagram (pin pb6) figure 62. port b schematic diagram (pin pb7) data bus d d q q reset reset c c wd wp rd mos pull- up pb6 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb6 portb6 spe mstr spi slave out spi master in rl rp data bus d d q q reset reset c c wd wp rd mos pull- up pb7 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb7 portb7 spe mstr spi cllock out spi clock in rl rp
98 atmega161(l) 1228d?avr?02/07 port c port c is an 8-bit bi-directional i/o port. three i/o memory address locations are allocated for the port c, one each for the data register ? portc, $15($35), data direction register ? ddrc, $14($34) and the port c input pins ? pinc, $13($33). the port c input pins address is read-only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port c output buffers can sink 20 ma and thus drive led displays directly. when pins pc0 to pc7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resis- tors are activated. the port c pins have alternate functions related to the optional external memory inter- face. port c can be configured to be the high-order address byte during accesses to external data memory. when port c is set to the alternate function by the sre (external sram enable) bit in the mcucr (mcu control register), the alternate settings override the data direction register. port c data register ? portc port c data direction register ? ddrc port c input pins address ? pinc the port c input pins address (pinc) is not a register; this address enables access to the physical value on each port c pin. when reading portc, the port c data latch is read and when reading pinc, the logical values present on the pins are read. port c as general digital i/o all eight pins in port c have equal fu nctionality when used as digital i/o pins. pcn, general i/o pin: the ddcn bit in the dd rc register selects the direction of this pin. if ddcn is set (one), pcn is configured as an output pin. if ddcn is cleared (zero), pcn is configured as an input pin. if portcn is set (one) when the pin is configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, portcn has to be cleared (zero) or the pin has to be configured as an output pin. the port c pins are tri-stated when a reset condition becomes active, even if the clock is not running. bit 76543210 $15 ($35) portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 portc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $14 ($34) ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 ddrc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $13 ($33) pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 pinc read/writerrrrrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
99 atmega161(l) 1228d?avr?02/07 note: 1. n: 7, 6,?0, pin number port c schematics note that all port pins are synchronized. the synchronization latch is, however, not shown in the figure. figure 63. port c schematic diagram (pins pc0 - pc7) table 32. ddcn effects on port c pins (1) ddcn portcn i/o pull-up comment 0 0 input no tri-state (high-z) 0 1 input yes pcn will source current if ext. pulled low 1 0 output no push-pull zero output 1 1 output no push-pull one output
100 atmega161(l) 1228d?avr?02/07 port d port d is an 8-bit bi-directional i/o port with internal pull-up resistors. three i/o address locations are allocated for the port d, one each for the data register ? portd, $12($32), data direction regist er ? ddrd, $11($31) and the port d input pins ? pind, $10($30). the port d input pins address is read-only, while the data reg- ister and the data direction register are read/write. the port d output buffers can sink 20 ma. as inputs, port d pins that are externally pulled low will source current if t he pull-up resistor s are activated. some port d pins have alternate functions as shown in table 33. when the pd5 pin is used for the alternate function (oc1a), the ddrd and portd registers have to be set according to the alternate function description. port d data register ? portd port d data direction register ? ddrd port d input pins address ? pind the port d input pins address (pind) is not a register; this address enables access to the physical value on each port d pin. when reading portd, the port d data latch is read and when reading pind, the logical values present on the pins are read. port d as general digital i/o pdn, general i/o pin: the dddn bit in the dd rd register selects the direction of this pin. if dddn is set (one), pdn is configured as an output pin. if dddn is cleared (zero), pdn is configured as an input pin. if portdn is set (one) when configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the portdn table 33. port d pin alternate functions port pin alternate function pd0 rxd0 (uart0 input line) pd1 txd0 (uart0 output line) pd2 int0 (external interrupt0 input) pd3 int1 (external interrupt1 input) pd3 tosc1 (rtc oscillator timer/counter2) pd5 tosc2 (rtc oscillator timer/counter2)/oc1a (timer/counter1 output comparea match output) pd6 wr (write strobe to external memory) pd7 rd (read strobe to external memory) bit 76543210 $12 ($32) portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 portd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $11 ($31) ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 ddrd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $10 ($30) pind7 pind6 pind5 pind4 pi nd3 pind2 pind1 pind0 pind read/writerrrrrrrr initial value n/a n/a n /a n/a n/a n/a n/a n/a
101 atmega161(l) 1228d?avr?02/07 has to be cleared (zero) or the pin has to be configured as an output pin. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. notes: 1. n: 7,6?0, pin number alternate functions of port d the alternate pin configuration is as follows: rd ? port d, bit 7 rd is the external data memory read control strobe. wr ? port d, bit 6 wr is the external data memory write control strobe. oc1 ? port d, bit 5 oc1, output compare match output: the pd5 pin can serve as an external output when the timer/counter1 compare matches. the pd5 pin has to be configured as an output (ddd5 set [one]) to serve this function. see ?timer/counter1? on page 49 for further details and how to enable the output. the oc1 pin is also the output pin for the pwm mode timer function.  tosc1/tosc2 ? port d, bits 5 and 4 when the as2 bit in assr is set (one) to enable asynchronous clocking of timer/counter2, pins pd5 and pd4 are disconnected from the port. in this mode, a crystal oscillator is connected to the pins and the pins cannot be used as i/o pins. int1 ? port d, bit 3 int1, external interrupt source 1: the pd3 pi n can serve as an external interrupt source to the mcu. see ?mcu control register ? mcucr? on page 34 for further details. int0 ? port d, bit 2 int0, external interrupt source 0: the pd2 pi n can serve as an external interrupt source to the mcu. see ?mcu control register ? mcucr? on page 34 for further details. txd0 ? port d, bit 1 transmit data (data output pin for the uart0). when the uart0 transmitter is enabled, this pin is configured as an output regardless of the value of ddrd1. rxd0 ? port d, bit 0 receive data (data input pin for the uart0). when the uart receiv er is enabled, this pin is configured as an input regardless of the value of ddrd0. when the uart0 forces this pin to be an input, a logical ?1? in portd0 will turn on the internal pull-up. table 34. dddn bits on port d pins (1) dddn portdn i/o pull-up comment 0 0 input no tri-state (high-z) 0 1 input yes pdn will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
102 atmega161(l) 1228d?avr?02/07 port d schematics note that all port pins are synchronized. the synchronization latches are, however, not shown in the figures. figure 64. port d schematic diagram (pin pd0) figure 65. port d schematic diagram (pin pd1) da ta bus d d q q reset reset c c wd wp rd mos pull- up pd0 rxd0 rxen0 wp: wd: rl: rp: rd: rxd0: rxen0: write portd write ddrd read portd latch read portd pin read ddrd uart0 receive data uart0 receive enable ddd0 portd0 rl rp da ta bus d d q q reset reset c c wd wp rd rp rl mos pull- up pd1 r r wp: wd: rl: rp: rd: txd0: txen0: write portd write ddrd read portd latch read portd pin read ddrd uart0 transmit data uart0 transmit enable ddd1 portd1 txen0 txd0
103 atmega161(l) 1228d?avr?02/07 figure 66. port d schematic diagram (pins pd2 and pd3) figure 67. port d schematic diagram (pin pd4) wp: wd: rl: rp: rd: n: m: write portd write ddrd read portd latch read portd pin read ddrd 2, 3 0, 1 da ta bus d d q q reset reset c c wd wp rd mos pull- up pd4 r r wp: wd: rl: rp: rd: as2: write portd write ddrd read portd latch read portd pin read ddrd asynch select t/c2 ddd4 portd4 rl rp as2 t/c2 osc amp input
104 atmega161(l) 1228d?avr?02/07 figure 68. port d schematic diagram (pin pd5) figure 69. port d schematic diagram (pin pd6) comp. match 1a foc1a pwm10 pwm11 wp: wd: rl: rp: rd: as2 write portd write ddrd read portd latch read portd pin read ddrd asynch select t/c2 wp: wd: rl: rp: rd: we: sre: write portd write ddrd read portd latch read portd pin read ddrd write enable external sram enable
105 atmega161(l) 1228d?avr?02/07 figure 70. port d schematic diagram (pin pd7) wp: wd: rl: rp: rd: re: sre: write portd write ddrd read portd latch read portd pin read ddrd read enable external sram enable
106 atmega161(l) 1228d?avr?02/07 port e port e is a 3-bit bi-directional i/o port with internal pull-up resistors. three i/o address locations are allocated for the port e, one each for the data register ? porte, $07($27), data direction register ? ddre, $06($26) and the port e input pins ? pine, $05($25). the port e input pins address is read-only, while the data reg- ister and the data direction register are read/write. the port e output buffers can sink 20 ma. as inputs, port e pins that are externally pulled low will source current if t he pull-up resistor s are activated. port e pins have alternate functions as shown in table 35. note: 1. when the pe1 pin is used for the alternate function, the ddre and porte registers have to be set according to the alternate function description. port e data register ? porte port e data direction register ? ddre port e input pins address ? pine the port e input pins address (pine) is not a register; this address enables access to the physical value on each port e pin. when reading porte, the port e data latch is read and when reading pine, the logical values present on the pins are read. port e as general digital i/o pen, general i/o pin: the dden bit in the ddre register selects the direction of this pin. if dden is set (one), pen is configured as an output pin. if dden is cleared (zero), pen is configured as an input pin. if porten is set (one) when configured as an input pin, the mos pull-up resistor is activated. to switch the pull-up resistor off, the porten has to be cleared (zero) or the pin has to be configured as an output pin. the port e pins are tri-stated when a reset condition becomes active, even if the clock is not running. table 35. port e pin alternate functions (1) port pin alternate function pe0 icp (input capture pin timer/counter1 )/int2 (external interrupt 2 input) pe1 oc1b (timer/counter1 output compareb match output) pe2 ale (address latch enable, external memory) bit 76543210 $07 ($27) ? ? ? ? ? porte2 porte1 porte0 porte read/write r r r r r r/w r/w r/w initial value 0 0 0 0 0 0 0 0 bit 76543210 $06 ($26) ? ? ? ? ? dde2 dde1 dde0 ddre read/writerrrrrr/wr/wr/w initial value00000000 bit 76543210 $05 ($25) ? ? ? ? ? pine2 pine1 pine0 pine read/write r r r r r r r r initial value 0 0 0 0 0 n/a n/a n/a
107 atmega161(l) 1228d?avr?02/07 note: 1. n: 2,1,0, pin number. alternate functions of port e the alternate pin configuration is as follows:  oc1b ? port e, bit 2 oc1b, output compare match output: the pe2 pin can serve as an external output when the timer/counter1 compare matches. the pe2 pin has to be configured as an output (dde2 set [one]) to serve this function. see ?timer/counter1? on page 49 for fur- ther details. the oc1b pin is also the output pin for the pwm mode timer function. ale ? port e, bit 1 ale: when the external memory is enabled, the pe1 pin serves as the dress latch enable. note that enabling of external memo ry will override both the direction and port value. see ?interface to external memory? on page 84 for a detailed description.  icp/int2 ? port e, bit 0 icp, input capture pin: the pe0 pin can serve as the input capture source for timer/counter 1. see page 54 for a detailed description. int2, external interrupt source 2: the pe0 pi n can serve as an external interrupt source to the mcu. see ?extended mcu control r egister ? emcucr? on page 36 for further details. table 36. dden bits on port e pins (1) dden porten i/o pull-up comment 0 0 input no tri-state (high-z) 0 1 input yes pen will source current if ext. pulled low. 1 0 output no push-pull zero output 1 1 output no push-pull one output
108 atmega161(l) 1228d?avr?02/07 port e schematics figure 71. port e schematic diagram (pin pe0) figure 72. port e schematic diagram (pin pe1) data bus d d q q reset reset c c wd wp rd mos pull- up pe0 r r wp: wd: rl: rp: rd: acic: aco: write porte write ddre read porte latch read porte pin read ddre comparator ic enable comparator output dde0 porte0 noise canceler edge select icf1 icnc1 ices1 0 1 acic aco rl rp d q c r porte0 isc2 '1' int2 sw clear hw clear data bus d d q q reset reset c c wd wp rd mos pull- up pe1 r r wp: wd: rl: rp: rd: sre: write porte write ddre read porte latch read porte pin read ddre dde1 porte1 rl rp sre ale xram enable ale: ale pulse from xram
109 atmega161(l) 1228d?avr?02/07 figure 73. port e schematic diagram (pin pe2) pe2 dde2 porte2 comp. match 1b com1b0 com1b1 wp: wd: rl: rp: rd: write porte write ddre read porte latch read porte pin read ddre foc1b pwm10 pwm11
110 atmega161(l) 1228d?avr?02/07 memory programming boot loader support the atmega161 provides a mechanism for downloading and uploading program code by the mcu itself. this feature allows flexible application software updates, controlled by the mcu using a flash-resident boot loader program. the atmega161 flash memory is organized in two main sections: 1. the application code section (address $0000 - $1dff) 2. the boot loader section/boot block (address $1e00 - $1fff) figure 74. memory sections the boot loader program can use any available data interface and associated protocol, such as uart serial bus interface, to input or output program code and write (program) that code into the flash memory or read the code from the program memory. the program flash memory is divided into pages that each contain 128 bytes. the boot loader flash section occupies eight pa ges from $1e00 to $1fff by 16-bit words. the store program memory (spm) instruction can access the entire flash, but it can only be executed from the boot loader flash section. if no boot loader capability is needed, the entire flash is available for application code. the atmega161 has two sep- arate sets of boot lock bits that can be set independently. this gives the user a unique flexibility to select differ ent levels of protection . the user can elect to: $0000 $1fff program memory application code section (7.5k x 16) $1e00 $1dff boot loader section (512 x 16)
111 atmega161(l) 1228d?avr?02/07  protect the entire flash from a software update by the boot loader program  only protect the boot loader section from a software update by the boot loader program  only protect the application code section from a software update by the boot loader program  allow software update in the entire flash see table 37 and table 38 for further details. the boot lock bits can be set in software and in serial or parallel programming mode, but they can only be cleared by a chip erase command. note: 1. ?1? = unprogrammed, ?0? = programmed note: 1. ?1? means unprogrammed, ?0? means programmed entering the boot loader program entering the boot loader takes place by a jump or call from the application program. this may be initiated by some trigger su ch as a command received via uart or spi interface. alternatively, the boot reset fuse (bootrst) can be programmed so that the reset vector is pointing to address $1e00 after a reset. in this case, the boot loader is started after the reset. after the application code is loaded, the program can start executing the application code. note that the fuses cannot be changed by the mcu itself. this means that once the boot reset fuse is programmed, the reset vector will always point to the boot loader reset and the fuse can only be changed through the serial or parallel programming interface. the bootrst fuse can also be locked by pro- gramming lb1. when lb1 is programmed it is not possible to change the bootrst fuse unless a chip erase command is performed first. table 37. boot lock bit0 protection modes (application code section) (1) blb0 mode blb02 blb01 protection 111 no restrictions for spm, lpm accessing application code section 210 spm is not allowed to write to the application code section. 300 spm is not allowed to write to the application code section, and lpm executing from the boot loader section is not allowed to read from the application code section. 401 lpm executing from the boot loader section is not allowed to read from the application code section. table 38. boot lock bit1 protection modes (boot loader section) (1) blb1 mode blb12 blb1 1protection 111 no restrictions for spm, lpm accessing boot loader section 2 1 0 spm is not allowed to write to the boot loader section. 300 spm is not allowed to write to the boot loader section, and lpm executing from the application code section is not allowed to read from the boot loader section. 401 lpm executing from the appl ication code section is not allowed to read from the boot loader section.
112 atmega161(l) 1228d?avr?02/07 note: 1. ?1? means unprogrammed, ?0? means programmed capabilities of the boot loader the program code within the boot loader section has the ability to read from and write into the entire flash, including the boot loader memory. this allows the user to update both the application code and the boot loader code that handles the software update. the boot loader can thus even modify itself, and it can also erase itself from the code if the feature is not needed anymore. special care must be taken if the user allows the boot loader section to be updated by leaving boot lock bit11 unprogrammed. an acci- dental write to the boot loader itself can corrupt the entire boot loader, and further software updates might be impossible. if it is not needed to change the boot loader software itself, it is recommended that the boot lock bit 11 be programmed to protect the boot loader software from software changes. self-programming the flash programming of the flash is executed one page at a time. the flash page must be erased first for correct programming. the general write lock (lock bit 2) does not con- trol the programming of the flash memory by spm instruction. similarly, the read/write lock (lock bit 1) does not control reading or writing by lpm/spm, if it is attempted. the program memory can only be updated page-by-page, not word by word. one page is 128 bytes (64 words). the program memory will be modified by first performing page erase, then by filling the tem porary page buffer one word at a time using spm, and then by executing page write. if only part of the page needs to be changed, the other parts must be stored (for example, in the temporary page buffer) before the erase, and then be rewritten. the temporary page buffer can be accessed in a random sequence. the cpu is halted both during page erase and dur ing page write and the spmen bit in the spmcr register will be auto-cle ared. for future compatibilit y, however, it is recom- mended that the user software verify that the spmen bit is cleared before starting a new page erase, page write, or before writing the lock bits command (see code examples below). it is essential that the page address used in both the page erase and page write operation is addressing the same page. setting the boot loader lock bits by spm to set the boot loader lock bits, write the desired data to r0, write ?1001? to spmcr, and execute spm within four clock cycles after writing spmcr. the only accessible lock bits are the boot lock bits that may prevent the application code and boot loader sections from any software update by the mcu. see table 37 and table 38 for how the different settings of the boot loader bits affect the flash access. if bit5 - bit2 in r0 is cleared (zero), t he corresponding boot lo ck bit will be programmed if an spm instruction is ex ecuted within four cycles after blbset and spmen are set in spmcr. performing page erase by spm to execute a page erase, set up the address in the z-pointer, write ?0011? to spmcr, and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 are ignored. the page address must be writt en to z13:z7. other bits in the z-pointer will be ignored during this operation. table 39. boot reset fuse, bootrst (1) bootrst reset address 1 reset vector = application reset (address $0000) 0 reset vector = boot loader reset (address $1e00) bit 76543210 ? ? blb12 blb11 blb02 blb01 ? ? r0
113 atmega161(l) 1228d?avr?02/07 fill the temporary buffer to write an instruction word, set up the addre ss in the z-pointer and data in r1:r0, write ?0001? to spmcr, and execute spm within four clock cycles after writing spmcr. the content of z6:z1 is used to address the data in the temporary buffer. z13:z7 must point to the page that is supposed to be written. perform a page write to execute a page write, set up the address in the z-pointer, write ?0101? to spmcr, and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 are ignored. the page address must be written to z13:z7. during this operation, z6:z0 must be zero to ensure that the page is written correctly. when a page write operation is completed, the z-pointer will point to the first word in the successive page. addressing the flash during self-programming the z-pointer is used to address the spm commands. z15:z14 always ignored z13:z7 page select, for page erase, page write z6:z1 word select, for filling te mp buffer (must be zero during page write operation) z0 should be zero for all spm commands, byte select for the lpm instruction the only operation that does not use the z-pointer is setting the boot loader lock bits. the content of the z- pointer is ignored and will have no effect on the operation. note that the page erase and page write operation are addressed independently. there- fore, it is of major importance that the boot loader software addresses the same page in both the page erase and page write operations. the lpm instruction also uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, the lsb (bit z0) of the z-pointer is also used. see page 16 for a detailed description. accidental writing into flash program by t he spm instruction is prevented by setting up an ?spm enable time window?. all accesses are executed by first setting i/o bits, and then by executing spm within four clock cycles. the i/o register that controls the spm accesses is defined below. code example wait: in r16,spmcr ; read spmcr register sbrc r16,spmen ; wait for spmen to be cleared (indicates that previous write operation is completed) rjmp wait ; if not cleared, keep waiting ldi r16,(1< 114 atmega161(l) 1228d?avr?02/07 store program memory control register ? spmcr the store program memory control register contains the control bits needed to control the programming of the flash from internal code execution.  bits 7..4 ? res: reserved bits these bits are reserved bits in the atmega161 and always read as zero. bit 3 ? blbset: boot lock bit set if this bit is set at the same time as spmen, the next spm instruction within four clock cycles sets boot lock bits according to the data in r0. the data in r1 and the address in the z-pointer are ig nored. the blbset bit will auto-cle ar upon completion of lock bit set, or if no spm instruction is executed wit hin four clock cycles. the cpu is halted dur- ing lock bit setting. only a chip erase can clear the lock bits. an lpm instruction within fo ur cycles after blbset and spmen are set in the spmcr register will put either the lock bits or the fuse bits (depending od t he z0 in the z- pointer) into the destination register. see ?reading the fuse and lock bits from soft- ware? for details. bit 2 ? pgwrt: page write if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon completion of a page writ e or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation. bit 1 ? pgers: page erase if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes a page erase. the page addres s is taken from the high part of the z- pointer. the data in r1 and r0 are ignored. the pgers bi t will auto-clear upon comple- tion of a page erase or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page erase operation. bit 0 ? spmen: store program memory enable this bit enables the spm instruction for the next four clock cycles. if set together with either blbset, pgwrt or pgers, the following spm instruction will have a special meaning (see description above). if only spmen is set, the following spm instruction will store the value in r1:r0 in the temporary page buffer addressed by the z-pointer. the lsb of the z-pointer is ignored. the spmen bit will auto-clear upon completion of an spm instruction or if no spm instruction is executed within four clock cycles. writing any combination other than ?1001?, ?0101?, ?0011? or ?0001? in the lower four bits, or writing to the i/o register wh en any bits are set, will have no effect. bit 7654 3 2 1 0 $37 ($57) ? ? blbset pgwrt pgers spmen spmcr read/writerrrrr/wr/wr/wr/w initial value 0 0 0 0 0 0 0 0
115 atmega161(l) 1228d?avr?02/07 eeprom write prevents writing to spmcr note that an eeprom write operation will block all software programming to flash. reading the fuse and lock bits from software will also be prevented during the eeprom write operation. it is recommended that the user check the status bit (eewe) in the eecr register and verify that the bi t is cleared before writing to the spmcr register. reading the fuse and lock bits from software it is possible to read both the fuse and lock bits from software. to read the lock bits, load the z-pointer with $0001 and set th e blbset and spmen bits in spmcr. if an lpm instruction is executed within three cpu cycles after the blbset and spmen bits are set in spmcr, the lock bits will be writ ten to the destinatio n register. the blbset and spmen bits will auto-clear upon comple tion of reading the lock bits or if no lpm/spm instruction is executed within three/four cpu cycles. when blbset and spmen are cleared, lpm will work as des cribed in ?constant addressing using the lpm instruction? on page 16 and in the instruction set manual. the algorithm for reading the fuse bits is similar to the one described above for reading the lock bits. but when reading the fuse bits, load $0000 in the z-pointer. when an lpm instruction is executed within three cycles after the blbset and spmen bits are set in the spmcr, the fuse bits can be read in the destination register as shown below. fuse and lock bits that are programmed will be read as zero. bit76543210 ? ? blb12 blb11 blb02 blb01 lb2 lb1 r0/rd bit 7 6 5 4 3 2 1 0 ? bootrst spien sut ? cksel[2] cksel[1] cksel[0] r0/rd
116 atmega161(l) 1228d?avr?02/07 program memory lock bits the atmega161 mcu provides six lock bits that can be left unprogrammed (?1?) or can be programmed (?0?) to obtain the additional features listed in table 40. the lock bits can only be erased to ?1? with the chip erase command. note: 1. program the fuse bits before programming the lock bits. fuse bits the atmega161 has six fuse bits: bo otrst, spien, sut, and cksel [2:0].  when bootrst is programmed (?0?), the reset vector is set to address $1e00, which is the first address location in the boot loader section of the flash. if the bootrst is unprogrammed (?1?), the reset vector is set to address $0000. default value is unprogrammed (?1?).  when the spien fuse is programmed (?0?), serial program and data downloading is enabled. default value is programmed (?0?). the spien fuse is not accessible in serial programming mode.  the sut fuse changes the start-up times. default value is unprogrammed (?1?). table 40. lock bit protection modes (1) memory lock bits protection type lb mode lb1 lb2 1 1 1 no memory lock features enabled 2 0 1 further programming of the flash and eeprom is disabled in parallel and serial programming modes. the fuse bits are locked in both serial and parallel programming modes. (1) 3 0 0 further programming and verification of the flash and eeprom is disabled in parallel and serial programming modes. the fuse bits are locked in both serial and parallel programming modes. (1) blb0 mode blb02 blb01 1 1 1 no restrictions for spm, lp m accessing the application code section 2 1 0 spm is not allowed to write to the application code section. 3 0 0 spm is not allowed to write to the application code section and lpm executing from boot loader section is not allowed to read from the application code section. 4 0 1 lpm executing from the b oot loader section is not allowed to read from the application code section. blb1 mode blb12 blb11 1 1 1 no restrictions for spm, lpm accessing the boot loader section 2 1 0 spm is not allowed to write the boot loader section. 3 0 0 spm is not allowed to write to the boot loader section and lpm executing from the application code section is not allowed to read from the boot loader section. 4 0 1 lpm executing from the application code section is not allowed to read from the boot loader section.
117 atmega161(l) 1228d?avr?02/07  cksel2..0: see table 4, ?reset delay selections (3) ,? on page 26, for which combination of cksel2..0 to use. default value is ?010?. the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) or lock bit2 (lb2) is programmed. program the fuse bits before programming the lock bits. signature bytes all atmel microcontrollers have a 3-byte signature code that identifies the device. this code can be read in both serial and parallel modes. the three bytes reside in a sepa- rate address space, and for the atmega161 they are: 1. $000: $1e (indicates manufactured by atmel) 2. $001: $94 (indicates 16 kb flash memory) 3. $002: $01 (indicates atmega161 device when $001 is $94) programming the flash and eeprom atmel?s atmega161 offers 16k bytes of in-system reprogrammable flash program memory and 512 bytes of eeprom data memory. the atmega161 is normally shipped with the on-chip flash program and eeprom data memory arrays in the erased state (i.e., contents = $ff) and ready to be pro- grammed. this device supports a high-volta ge (12v) parallel programming mode and a low-voltage serial programming mode. the +12v is used for programming enable only, and no current of significance is drawn by th is pin. the serial programming mode pro- vides a convenient way to download the program and data into the atmega161 inside the user?s system. the program memory array on the atmega161 is organized as 128 pages of 128 bytes each. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simultaneously in either program- ming mode. the eeprom data memory array on the atmega161 is programmed byte-by-byte in either programming mode. an auto-erase cycle is provided with the self-timed eeprom programming operation in the serial programming mode. during programming, the supply voltage must be in accordance with table 41. table 41. supply voltage during programming part serial programming parallel programming atmega161l 2.7 - 5.5v 4.5 - 5.5v atmega161 4.0 - 5.5v 4.5 - 5.5v
118 atmega161(l) 1228d?avr?02/07 parallel programming this section describes how to parallel program and verify flash program memory, eeprom data memory, lock bits and fuse bits in the atmega161. pulses are assumed to be at least 500 ns unless otherwise noted. signal names in this section, some pins of the atmega1 61 are referenced by signal names describing their functionality during parallel programming (see figure 75 and table 42). pins not described in the following table are referenced by pin name. the xa1/xa0 pins determine the action executed when the xtal1 pin is given a posi- tive pulse. the bit codings are shown in table 43. when pulsing wr or oe , the command loaded determines the action executed. the command is a byte where the different bits are assigned functions as shown in table 44. figure 75. parallel programming table 42. pin name mapping signal name in programming mode pin name i/o function rdy/bsy pd1 o 0: device is busy programming; 1: device is ready for new command oe pd2 i output enable (active low) wr pd3 i write pulse (active low) bs1 pd4 i byte select 1 (?0? se lects low byte, ?1? selects high byte) xa0 pd5 i xtal action bit 0 xa1 pd6 i xtal action bit 1 atmega161 vcc +5v gnd xtal1 pd1 pd2 pd3 pd4 pd5 pd6 pb7 - pb0 data reset pd7 +12 v bs1 xa0 xa1 oe rdy/bsy pagel pa0 wr
119 atmega161(l) 1228d?avr?02/07 enter programming mode the following algorithm puts the devi ce in parallel programming mode: 1. apply 4.5 - 5.5v between v cc and gnd. 2. set reset and bs pins to ?0? and wait at least 500 ns. 3. apply 11.5 - 12.5v to reset , and wait for at least 500 ns. chip erase the chip erase will erase the flash and eeprom memories plus lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be performed before the flash is reprogrammed. load command ?chip erase? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. 3. set data to ?1000 0000?. this is the command for chip erase. 4. give wr a negative pulse. this starts the chip erase. rdy/bsy goes low. 5. wait until rdy/bsy goes high before loading a new command. pagel pd7 i program memory page load bs2 pa0 i byte select 2 (always low) data pb7 - 0 i/o bi-directional data bus (output when oe is low) table 43. xa1 and xa0 coding xa1 xa0 action when xtal1 is pulsed 00 load flash or eeprom address (high or low address byte determined by bs1) 01 load data (high or low data byte for flash determined by bs1) 10 load command 11 no action, idle table 44. command byte bit coding command byte command executed 1000 0000 chip erase 0100 0000 write fuse bits 0010 0000 write lock bits 0001 0000 write flash 0001 0001 write eeprom 0000 1000 read signature bytes 0000 0100 read fuse and lock bits 0000 0010 read flash 0000 0011 read eeprom table 42. pin name mapping signal name in programming mode pin name i/o function
120 atmega161(l) 1228d?avr?02/07 programming the flash the flash is organized as 128 pages of 12 8 bytes each. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simultaneously. the following procedure describes how to program the entire flash memory: a. load command ?write flash? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. 3. set data to ?0001 0000?. this is the command for write flash. 4. give xtal1 a positive pulse. this loads the command. b. load address low byte 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?0?. this selects low address. 3. set data = address low byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the address low byte. c. load data low byte 1. set bs1 to ?0?. this selects low data byte. 2. set xa1, xa0 to ?01?. this enables data loading. 3. set data = data low byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the data byte. d. latch data low byte give pagel a positive pulse. this latches the data low byte. (see figure 76 for signal waveforms.) e. load data high byte 1. set bs1 to ?1?. this selects high data byte. 2. set xa1, xa0 to ?01?. this enables data loading. 3. set data = data high byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the data byte. f. latch data high byte give pagel a positive pulse. this latches the data high byte. g. repeat ?b? throug h ?f? 64 times to fill the page buffer. to address a page in the flash, seven bits are needed (128 pages). the five most sig- nificant bits are read from address high byte as described in section ?h? below. the two least significant page address bits, however, ar e the two most significant bits (bit7 and bit6) of the latest loaded address low byte as described in section ?b?. h. load address high byte 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?1?. this selects high address. 3. set data = address high byte ($00 - $1f). 4. give xtal1 a positive pulse. this loads the address high byte. i. program page
121 atmega161(l) 1228d?avr?02/07 1. give wr a negative pulse. this starts programming of the entire page of data. rdy/bsy goes low. 2. wait until rdy/bsy goes high. (see figure 77 for signal waveforms.) j. end page programming 1. set xa1, xa0 to ?10?. this enables command loading. 2. set data to ?0000 0000?. this is the command for no operation. 3. give xtal1 a positive pulse. this loads the command, and the internal write sig- nals are reset. k. repeat ?a? through ?j? 128 times or until all data have been programmed. figure 76. programming the flash waveforms figure 77. programming the flash waveforms (continued) $10 addr. low addr. high data low data xa1 xa2 bs1 xtal1 rdy/bsy reset wr oe +12v bs2 pagel data high data xa1 xa0 bs1 xtal1 wr rdy/bsy reset +12v oe bs2 pagel
122 atmega161(l) 1228d?avr?02/07 programming the eeprom the programming algorithm for the eeprom data memory is as follows (refer to ?pro- gramming the flash? for details on command, address and data loading): 1. a: load command ?0001 0001?. 2. h: load address high byte ($00 - $01) 3. b: load address low byte ($00 - $ff) 4. e: load data low byte ($00 - $ff) l: write data low byte 1. set bs to ?0?. this selects low data. 2. give wr a negative pulse. this starts programming of the data byte. rdy/bsy goes low. 3. wait until to rdy/bsy goes high before programming the next byte. (see figure 78 for signal waveforms.) the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered:  the command needs to be loaded only once when writing or reading multiple memory locations.  address high byte only needs to be loaded before programming a new 256-word page in the eeprom.  skip writing the data value $ff, that is, the contents of the entire eeprom after a chip erase. these considerations also apply to flash, eeprom an d signature by tes reading. figure 78. programming the eeprom waveforms $11 addr. high addr. low data low data xa1 xa2 bs1 xtal1 rdy/bsy reset wr oe +12v bs2 pagel
123 atmega161(l) 1228d?avr?02/07 reading the flash the algorithm for reading the flash memory is as follows (refer to ?programming the flash? on page 120 for details on command and address loading): 1. a: load command ?0000 0010?. 2. h: load address high byte ($00 - $1f) 3. b: load address low byte ($00 - $ff) 4. set oe to ?0?, and bs1 to ?0?. the flash word low byte can now be read at data. 5. set bs to ?1?. the flash word high byte can now be read at data. 6. set oe to ?1?. reading the eeprom the algorithm for readi ng the eeprom memory is as follows (refer to ?programming the flash? on page 120 for details on command and address loading): 1. a: load command ?0000 0011?. 2. h: load address high byte ($00 - $01) 3. b: load address ($00 - $ff) 4. set oe to ?0?, and bs1 to ?0?. the eepr om data byte ca n now be read at data. 5. set oe to ?1?. programming the fuse bits the algorithm for programming the fuse bits is as follows (refer to ?programming the flash? on page 120 for details on command and data loading): 1. a: load command ?0100 0000?. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. bit 6 = bootrst fuse bit bit 5 = spien fuse bit bit 4 = sut fuse bit bit 3 = ?1?. this bit is reserved and must be left unprogrammed (?1?). bits 2 - 0 = cksel2..0 fuse bits bit 7 = ?1?. this bit is reserved and should be left unprogrammed (?1?). 3. give wr a negative pulse and wait for rdy/bsy to go high. programming the lock bits the algorithm for programming the lock bits is as follows (refer to ?programming the flash? on page 120 for details on command and data loading): 1. a: load command ?0010 0000?. 2. d: load data low byte. bit n = ?0? programs the lock bit. bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 bits 7 - 6 = ?1?. these bits are reserved and should be left unprogrammed (?1?). 3. l: write data low byte. the lock bits can only be cleared by executing chip erase.
124 atmega161(l) 1228d?avr?02/07 reading the fuse and lock bits the algorithm for reading the fuse and lock bits is as follows (refer to ?programming the flash? on page 120 for details on command loading): 1. a: load command ?0000 0100?. 2. set oe to ?0?, and bs to ?0?. the status of the fuse bits can now be read at data (?0? means programmed). bit 6 = bootrst fuse bit bit 5 = spien fuse bit bit 4 = sut fuse bit bit 3 = ?1?. this bit is reserved and must be left unprogrammed (?1?). bits 2 - 0 = cksel2..0 fuse bits 3. set oe to ?0?, and bs to ?1?. the status of the lock bits can now be read at data (?0? means programmed). bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 4. set oe to ?1?. reading the signature bytes the algorithm for reading the signature bytes is as follows (refer to ?programming the flash? on page 120 for details on command and address loading): 1. a: load command ?0000 1000?. 2. c: load address low byte ($00 - $02). set oe to ?0?, and bs to ?0?. the selected signature byte can now be read at data. 3. set oe to ?1?. parallel programming characteristics figure 79. parallel programming timing data & control (data, xa0/1, bs1) data write read xtal1 t xhxl t wlwh t dvxh t xlol t oldv t xldx t plwl t wlrh wr rdy/bsy oe pagel t phpl t plbx t bvxh t xlwl t rhbx t ohdz t bvwl wlrl
125 atmega161(l) 1228d?avr?02/07 notes: 1. t wlrh is valid for the write eeprom, write fuse bits and write lock bits commands. 2. t wlrh_ce is valid for the chip erase command. 3. t wlrh_flash is valid for the write flash command. serial downloading both the flash and eeprom memory arrays can be programmed using the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (o utput). after reset is set low, the programming enable instruction needs to be executed first, before program/erase operations can be executed. when programming the eeprom, an auto-erase cy cle is built into the self-timed pro- gramming operation (in the serial mode only ) and there is no need to first execute the chip erase instruction. the chip erase operation turns the contents of every memory location in both the program and eeprom arrays into $ff. the program and eeprom me mory arrays have sepa rate address spaces: $0000 to $1fff for program memory and $0000 to $01ff for eeprom memory. either an external system clock is supplied at pin xtal1 or a crystal needs to be con- nected across pins xtal1 and xtal2. the minimum low and high periods for the serial clock (sck) input are defined as follows: low: > 2 xtal1 clock cycles high: > 2 xtal1 clock cycles table 45. parallel programming characteristics, t a = 25 c 10%, v cc = 5v 10% (1)(2)(3) symbol parameter min typ max units v pp programming enable voltage 11.5 12.5 v i pp programming enable current 250 a t dvxh data and control valid before xtal1 high 67 ns t xhxl xtal1 pulse width high 67 ns t xldx data and control hold after xtal1 low 67 ns t xlwl xtal1 low to wr low 67 ns t bvxh bs1 valid before xtal1 high 67 ns t phpl pagel pulse width high 67 ns t plbx bs1 hold after pagel low 67 ns t plwl pagel low to wr low 67 ns t bvwl bs1 valid to wr low 67 ns t rhbx bs1 hold after rdy/bsy high 67 ns t wlwh wr pulse width low 67 ns t wlrl wr low to rdy/bsy low 0 2.5 s t wlrh wr low to rdy/bsy high (1) 11.7ms t wlrh_ce wr low to rdy/bsy high for chip erase (2) 16 28 ms t wlrh_flash wr low to rdy/bsy high for write flash (3) 814ms t xlol xtal1 low to oe low 67 ns t oldv oe low to data valid 20 ns t ohdz oe high to data tri-stated 20 ns
126 atmega161(l) 1228d?avr?02/07 serial programming algorithm when writing serial data to the atmega161, data is clocked on the rising edge of sck. when reading data from th e atmega161, data is clocked on the falling edge of sck. see figure 80, figure 81 and table 49 for timing details. to program and verify the atmega161 in the serial programming mode, the following sequence is recommended (see 4-byte instruction formats in table 48 ): 1. power-up sequence: apply power between v cc and gnd while reset and sck are set to ?0?. if a crys- tal is not connected across pins xtal1 and xtal2, apply a clock signal to the xtal1 pin. in some systems, the programmer cannot guarantee that sck is held low during power-up. in this case, reset must be given a positive pulse of at least two xtal1 cycles? duration after sck has been set to ?0?. 2. wait for at least 20 ms and enable serial programming by sending the program- ming enable serial instruction to pin mosi/pb5. 3. the serial programming instructions will not work if the communication is out of synchronization. when in sync, the seco nd byte ($53) will echo back when issu- ing the third byte of the programming enable instruction. whether or not the echo is correct, all four bytes of the instruction must be transmitted. if the $53 did not echo back, give reset a positive pulse and issue a new programming enable command. 4. if a chip erase is performed (must be done to erase the flash), give reset a positive pulse and start over from step 2. 5. the flash is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 6 lsb of the address and data together with the load program memory page instruction. the program memory page is stored by loading the write program memory page instruction with the 7 msb of the address. if polling is not used, the user must wait at least t wd_flash before issu- ing the next page (please refer to table 46). accessing the serial programming interface before the flash write operation completes can result in incorrect programming. 6. the eeprom array is programmed one byte at a time by supplying the address and data together with th e appropriate write instru ction. an eeprom memory location is first automatica lly erased before new data is written. if polling is not used, the user must wait at least t wd_eeprom before issuing the next byte (please refer to table 46). in a chip-erased device, no $ffs in the data file(s) need to be programmed. 7. any memory location can be verified by using the read instruction, which returns the content at the selected address at serial output miso/pb6. 8. at the end of the programming session, reset can be set high to commence normal operation. 9. power-off sequence (if needed): set xtal1 to ?0? (if a crystal is not used). set reset to ?1?. tu r n v cc power off. data polling flash when a page is being programmed into the flash, reading an address location within the page being programmed will gi ve the value $ff. at the time the device is ready for a new page, the pr ogrammed value will read correctly. this is used to de termine when the next page can be written. note that the entire page is written simultaneously and any address within the page can be used for pollin g. data polling of the flash will not work for the value $ff, so when pr ogramming this value, the user will have to wait for at least
127 atmega161(l) 1228d?avr?02/07 t wd_flash before programming the next page. as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff can be skipped. see table 46 for t wd_flash value. data polling eeprom when a new byte has been written and is being programmed into eeprom, reading the address location being program med will give the value $ff. at the time the device is ready for a new byte, the programmed value will read correctly. this is used to deter- mine when the next byte can be written. this will not work for the value $ff, but the user should keep the following in mind: as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff can be skipped. this does not apply if the eeprom is reprogrammed without chip-erasing the device. in this case, data polling cannot be used for the value $ff, and the user will have to wait at least t wd_eeprom before programming the next byte. see table 46 for t wd_eeprom value. figure 80. serial programm ing waveforms table 46. minimum wait delay befo re writing the next fl ash or eeprom location symbol minimum wait delay t wd_flash 14 ms t wd_eeprom 3.4 ms table 47. minimum wait delay afte r a chip erase command symbol minimum wait delay t wd_erase 28 ms msb msb lsb lsb serial clock input pb7(sck) serial data input pb5 (mosi) pb6 (miso) sample serial data output
128 atmega161(l) 1228d?avr?02/07 . note: 1. a = address high bits b = address low bits h = 0 ? low byte, 1 ? high byte o = data out i = data in x = don?t care 1 = lock bit 1 2 = lock bit 2 3 = boot lock bit1 4 = boot lock bit2 5 = boot lock bit11 6 = boot lock bit12 7 = cksel0 fuse 8 = cksel1 fuse 9 = cksel2 fuse b = sut fuse c = spien fuse d = bootrst fuse table 48. serial programming instruction set (1) instruction instruction format operation byte 1 byte 2 byte 3 byte 4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 xxxa aaaa bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a : b . load program memory page 0100 h 000 xxxx xxxx xx bb bbbb iiii iiii write h (high or low) data i to program memory page at word address b . write program memory page 0100 1100 xxxa aaaa bb xx xxxx iiii iiii write program memory page at address a : b . read eeprom memory 1010 0000 xxxx xxx a bbbb bbbb oooo oooo read data o from eeprom memory at address a : b . write eeprom memory 1100 0000 xxxx xxx a bbbb bbbb iiii iiii write data i to eeprom memory at address a : b . read lock bits 0101 1000 xxxx xxxx xxxx xxxx xx 65 4321 read lock bits. ?0? = programmed, ?1? = unprogrammed. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 65 4321 write lock bits. set bits 6 - 1 = ?0? to program lock bits. read signature byte 0011 0000 xxxx xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 101x xxxx xxxx xxxx 1 d 1 b 1987 set bits d - b , 9 - 7 = ?0? to program, ?1? to unprogram read fuse bits 0101 0000 xxxx xxxx xxxx xxxx x d c b 1987 read fuse bits. ?0? = programmed, ?1? = unprogrammed
129 atmega161(l) 1228d?avr?02/07 serial programming characteristics figure 81. serial programming timing table 49. serial programming characteristics, t a = -40 c to 85 c, v cc = 2.7 - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (v cc = 2.7 - 5.5v )0 4mhz (v cc = 4.0 - 5.5v )0 8mhz t clcl oscillator period (v cc = 2.7 - 5.5v ) 250 ns (v cc = 4.0 - 5.5v ) 125 ns t shsl sck pulse width high 2 t clcl ns t slsh sck pulse width low 2 t clcl ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid 10 16 32 ns mosi miso sck t ovsh t shsl t slsh t shox t sliv
130 atmega161(l) 1228d?avr?02/07 electrical characteristics absolute maximum ratings* operating temperature.................................. -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature ..................................... -65 c to +150 c voltage on any pin except reset with respect to ground .............................-1.0v to v cc + 0.5v voltage on reset with respect to ground ....-1.0v to +13.0v maximum operating voltage ............................................ 6.6v dc current per i/o pin ............................................... 40.0 ma dc current vcc and gnd pins ............................... 200.0 ma
131 atmega161(l) 1228d?avr?02/07 notes: 1. ?max? means the highest value where the pin is guaranteed to be read as low. 2. ?min? means the lowest value where t he pin is guaranteed to be read as high. 3. although each i/o port can sink more than the test conditions (20 ma at v cc = 5v, 10 ma at v cc = 3v) under steady-state conditions (non-transient), th e following must be observed: 1] the sum of all i ol , for all ports, should not exceed 200 ma. 2] the sum of all i ol , for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all i ol , for ports a0 - a7, ale, oc1b and c0 - c7 should not exceed 100 ma. if i ol exceeds the test condition, v ol may exceed the related specification. pi ns are not guaranteed to sink current greater than the listed test condition. 4. although each i/o port can source more than the test conditions (3 ma at v cc = 5v, 1.5 ma at v cc = 3v) under steady-state conditions (non-transient), th e following must be observed: 1] the sum of all i oh , for all ports, should not exceed 200 ma. 2] the sum of all i oh , for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all i oh , for ports a0 - a7, ale, oc1b and c0 - c7 should not exceed 100 ma. dc characteristics t a = -40 c to 85 c, v cc = 2.7v to 5.5v (unless otherwise noted) (1)(2)(3)(4)(5) symbol parameter condition min typ max units v il input low voltage (except xtal1) -0.5 0.3 v cc (1) v v il1 input low voltage (xtal1) -0.5 0.2 v cc (1) v v ih input high voltage (except xtal1, reset ) 0.6 v cc (2) v cc + 0.5 v v ih1 input high voltage (xtal1) 0.8 v cc (2) v cc + 0.5 v v ih2 input high voltage (reset ) 0.9 v cc (2) v cc + 0.5 v v ol output low voltage (3) (ports a,b,c,d) i ol = 20 ma, v cc = 5v i ol = 10 ma, v cc = 3v 0.6 0.5 v v v oh output high voltage (4) (ports a,b,c,d) i oh = -3 ma, v cc = 5v i oh = -1.5 ma, v cc = 3v 4.2 2.3 v v i il input leakage current i/o pin v cc = 5.5v, pin low (absolute value) 8.0 a i ih input leakage current i/o pin v cc = 5.5v, pin high (absolute value) 980 na rrst reset pull-up resistor 100 500 k ? r i/o i/o pin pull-up resistor 35 120 k ? i cc power supply current active mode, v cc = 3v, 4mhz 3.0 ma idle mode v cc = 3v, 4mhz 1.2 ma power-down mode (5) wdt enabled, v cc = 3v 9 15.0 a wdt disabled, v cc = 3v <1 2.0 a v acio analog comparator input offset voltage v cc = 5v v in = v cc /2 40 mv i aclk analog comparator input leakage current v cc = 5v v in = v cc /2 -50 50 na t acpd analog comparator propagation delay v cc = 2.7v v cc = 4.0v 750 500 ns
132 atmega161(l) 1228d?avr?02/07 if i oh exceeds the test condition, v oh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. 5. minimum v cc for power-down is 2v. external clock drive waveforms figure 82. external clock notes: 1. see ?external data memo ry timing? for a description of how the duty cycle influences the timing for the external data memory. table 50. external clock drive (1) symbol parameter v cc = 2.7v to 5.5 vv cc = 4.0v to 5.5 v units minmaxminmax 1/t clcl oscillator frequency 0408mhz t clcl clock period 250 125 ns t chcx high time 100 50 ns t clcx low time 100 50 ns t clch rise time 1.6 0.5 s t chcl fall time 1.6 0.5 s vil1 vih1
133 atmega161(l) 1228d?avr?02/07 external data memory timing notes: 1. this assumes 50% clock duty cycl e. the half peri od is actually the high time of the external clock, xtal1. 2. this assumes 50% clock duty cycle. the half period is actually the low time of the external clock, xtal1. table 51. external data memory characteristics, 4.0 - 5.5 volts, no wait state symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 1t lhll ale pulse width 95 1.0t clcl -30 ns 2t avll address valid a to ale low 22.5 0.5t clcl -40 (1) ns 3a t llax_st address hold after ale low, write access 10 10 ns 3b t llax_ld address hold after ale low, read access 15 15 ns 4t avllc address valid c to ale low 22.5 0.5t clcl -40 (1) ns 5t avrl address valid to rd low 95 1.0t clcl -30 ns 6t avwl address valid to wr low 95 1.0t clcl -30 ns 7t llwl ale low to wr low 42.5 145 0.5t clcl -20 (2) 0.5t clcl +20 (2) ns 8t llrl ale low to rd low 42.5 145 0.5t clcl -20 (2) 0.5t clcl +20 (2) ns 9t dvrh data setup to rd high 60 60 ns 10 t rldv read low to data valid 65 65 ns 11 t rhdx data hold after rd high 0 0 ns 12 t rlrh rd pulse width 105 1.0t clcl -20 ns 13 t dvwl data setup to wr low 27.5 0.5t clcl -35 (1) ns 14 t whdx data hold after wr high 27.5 0.5t clcl -35 (1) ns 15 t dvwh data valid to wr high 95 1.0t clcl -30 ns 16 t wlwh wr pulse width 105 1.0t clcl -20 ns table 52. external data memory characteristics, 4.0 - 5.5 volts, 1 cycle wait state symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid 185 2.0t clcl -65 ns 12 t rlrh rd pulse width 230 2.0t clcl -20 ns 15 t dvwh data valid to wr high 220 2.0t clcl -30 ns 16 t wlwh wr pulse width 230 2.0t clcl -20 ns
134 atmega161(l) 1228d?avr?02/07 table 53. external data memory characteristics, 4.0 - 5.5 volts, srwn1 = 1, srwn0 = 0 symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid 310 3.0t clcl -65 ns 12 t rlrh rd pulse width 355 3.0t clcl -20 ns 15 t dvwh data valid to wr high 345 3.0t clcl -30 ns 16 t wlwh wr pulse width 35 3.0t clcl -20 ns table 54. external data memory characteristics, 4.0 - 5.5 volts, srwn1 = 1, srwn0 = 1 symbol parameter 8 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 8.0 mhz 10 t rldv read low to data valid 310 3.0t clcl -65 ns 12 t rlrh rd pulse width 355 3.0t clcl -20 ns 14 t whdx data hold after wr high 152.5 1.5t clcl -35 ns 15 t dvwh data valid to wr high 345 3.0t clcl -30 ns 16 t wlwh wr pulse width 355 3.0t clcl -20 ns table 55. external data memory characteristics, 2.7 - 5.5 volts, no wait state symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 1t lhll ale pulse width 195 t clcl -55 ns 2t avll address valid a to ale low 60 0.5t clcl -65 ns 3a t llax_st address hold after ale low, write access 10 10 ns 3b t llax_ld address hold after ale low, read access 15 15 ns 4t avllc address valid c to ale low 60 0.5t clcl -65 ns 5t avrl address valid to rd low 200 1.0t clcl -50 ns 6t avwl address valid to wr low 200 1.0t clcl -50 ns 7t llwl ale low to wr low 105 145 0.5t clcl -20 0.5t clcl +20 ns 8t llrl ale low to rd low 105 145 0.5t clcl -20 0.5t clcl +20 ns 9t dvrh data setup to rd high 95 95 ns 10 t rldv read low to data valid 165 165 ns 11 t rhdx data hold after rd high 0 0 ns
135 atmega161(l) 1228d?avr?02/07 notes: 1. this assumes 50% clock duty cycl e. the half peri od is actually the high time of the external clock, xtal1. 2. this assumes 50% clock duty cycle. the half period is actually the low time of the external clock, xtal1. 12 t rlrh rd pulse width 230 1.0t clcl -20 ns 13 t dvwl data setup to wr low 70 0.5t clcl -55 ns 14 t whdx data hold after wr high 125 0.5t clcl -0 ns 15 t dvwh data valid to wr high 210 1.0t clcl -40 ns 16 t wlwh wr pulse width 230 1.0t clcl -20 ns table 55. external data memory characteristics, 2.7 - 5.5 volts, no wait state (continued) symbol parameter 4 mhz oscillator variable oscillator unit min max min max table 56. external data memory characteristics, 2.7 - 5.5 volts, srwn1 = 0, srwn0 = 1 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid 335 2.0t clcl -165 ns 12 t rlrh rd pulse width 480 2.0t clcl -20 ns 15 t dvwh data valid to wr high 460 2.0t clcl -40 ns 16 t wlwh wr pulse width 480 2.0t clcl -20 ns table 57. external data memory characteristics, 2.7 - 5.5 volts, srwn1 = 1, srwn0 = 0 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid 585 3.0t clcl -165 ns 12 t rlrh rd pulse width 730 3.0t clcl -20 ns 15 t dvwh data valid to wr high 710 3.0t clcl -40 ns 16 t wlwh wr pulse width 730 3.0t clcl -20 ns table 58. external data memory characteristics, 2.7 - 5.5 volts, srwn1 = 1, srwn0 = 1 symbol parameter 4 mhz oscillator variable oscillator unit min max min max 01/t clcl oscillator frequency 0.0 4.0 mhz 10 t rldv read low to data valid 585 3.0t clcl -165 ns 12 t rlrh rd pulse width 730 3.0t clcl -20 ns 14 t whdx data hold after wr high 375 1.5t clcl -0 ns 15 t dvwh data valid to wr high 710 3.0t clcl -40 ns 16 t wlwh wr pulse width 730 3.0t clcl -20 ns
136 atmega161(l) 1228d?avr?02/07 figure 83. external memory timing (srwn1 = 0, srwn0 = 0) figure 84. external memory timing (srwn1 = 0, srwn0 = 1) ale wr rd address [15..8] address xx xx read data/address [7..0] address data xx write data/address [7..0] address data xx system clock ? t1 t2 t3 t4 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data ale wr rd address [15..8] address xx xx read data/address [7..0] address data xx write data/address [7..0] address data xx t1 t2 t3 t5 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data system clock ? t4
137 atmega161(l) 1228d?avr?02/07 figure 85. external memory timing (srwn1 = 1, srwn0 = 0) figure 86. external memory timing (srwn1 = 1, srwn0 = 1) (1) note: 1. the ale pulse in the last period (t4 - t7) is only present if the next instruction accesses the ram (internal or external). the data and address will only change in t4 - t7 if ale is present (the next instruction accesses the ram). ale wr rd address [15..8] address xx xx read data/address [7..0] address data xx write data/address [7..0] address data xx t1 t2 t3 t6 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data system clock ? t4 t5 ale wr rd address [15..8] address xx xx read data/address [7..0] address data xx write data/address [7..0] address data xx t1 t2 t3 t7 1 4 2 7 6 3a 3b 5 8 12 16 13 15 9 10 11 14 prev. addr. xx prev. data xx prev. data t4 t5 t6 system clock ?
138 atmega161(l) 1228d?avr?02/07 typical characteristics analog comparator offset voltage is measured as absolute offset. figure 87. analog comparator offset voltage vs. common mode voltage figure 88. analog comparator offset voltage vs. common mode voltage 0 2 4 6 8 10 12 14 16 18 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 analog comparator offset voltage vs. common mode voltage v cc = 5v common mode voltage (v) offset voltage (mv) t a = 25?c t a = 85?c 0 2 4 6 8 10 0 0.5 1 1.5 2 2.5 3 analog comparator offset voltage vs. common mode voltage v cc = 2.7v common mode voltage (v) offset voltage (mv) t a = 25?c t a = 85?c
139 atmega161(l) 1228d?avr?02/07 figure 89. analog comparator input leakage current figure 90. watchdog oscillator frequency vs. v cc sink and source capabilities of i/o po rts are measured on one pin at a time. 60 50 40 30 20 10 0 -10 0 0.5 1.5 1 2 2.5 3.5 3 4 4.5 5 6 6.5 7 5.5 analog comparator input leakage current v cc = 6v t a = 25?c i aclk (na) v in (v) 0 200 400 600 800 1000 1200 1400 1600 2 2.5 3 3.5 4 4.5 5 5.5 6 watchdog oscillator frequency vs. v cc v cc (v) f rc (khz) t a = 25?c t a = 85?c
140 atmega161(l) 1228d?avr?02/07 figure 91. pull-up resistor current vs. input voltage figure 92. pull-up resistor current vs. input voltage 0 20 40 60 80 100 120 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 pull-up resistor current vs. input voltage v cc = 5v t a = 25?c t a = 85?c i op (a) v op (v) 0 5 10 15 20 25 30 0 0.5 1 1.5 2 2.5 3 pull-up resistor current vs. input voltage v cc = 2.7v t a = 25?c t a = 85?c i op (a) v op (v)
141 atmega161(l) 1228d?avr?02/07 figure 93. i/o pin sink current vs. output voltage figure 94. i/o pin source current vs. output voltage 0 10 20 30 40 50 60 70 0 0.5 1 1.5 2 2.5 3 i/o pin sink current vs. output voltage v cc = 5v t a = 25?c t a = 85?c i ol (ma) v ol (v) 0 2 4 6 8 10 12 14 16 18 20 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i/o pin source current vs. output voltage v cc = 5v t a = 25?c t a = 85?c i oh (ma) v oh (v)
142 atmega161(l) 1228d?avr?02/07 figure 95. i/o pin sink current vs. output voltage figure 96. i/o pin source current vs. output voltage 0 5 10 15 20 25 0 0.5 1 1.5 2 i/o pin sink current vs. output voltage v cc = 2.7v t a = 25?c t a = 85?c i ol (ma) v ol (v) 0 1 2 3 4 5 6 0 0.5 1 1.5 2 2.5 3 i/o pin source current vs. output voltage v cc = 2.7v t a = 25?c t a = 85?c i oh (ma) v oh (v)
143 atmega161(l) 1228d?avr?02/07 figure 97. i/o pin input threshold vs. v cc figure 98. i/o pin input hysteresis vs. v cc 0 0.5 1 1.5 2 2.5 2.7 4.0 5.0 threshold voltage (v) v cc i/o pin input threshold voltage vs. v cc t a = 25?c 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 2.7 4.0 5.0 iinput hysteresis (v) v cc i/o pin input hysteresis vs. v cc t a = 25?c
144 atmega161(l) 1228d?avr?02/07 register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page $3f ($5f) sreg i t h s v n z c page 21 $3e ($5e) sph sp15 sp14 sp13 sp12 sp11 sp10 sp9 sp8 page 22 $3d ($5d) spl sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 page 22 $3c ($5c) reserved $3b ($5b) gimsk int1 int0 int2 - - - - - page 30 $3a ($5a) gifr intf1 intf0 intf2 page 31 $39 ($59) timsk toie1 ocie1a ocie1b toie2 ticie1 ocie2 toie0 ocie0 page 31 $38 ($58) tifr tov1 ocf1a ocf1b tov2 icf1 ocfi2 tov0 ocif0 page 33 $37 ($57) spmcr - - - - lbset pgwrt pgers spmen page 114 $36 ($56) emcucr sm0 srl2 srl1 srl0 srw01 srw00 srw11 isc2 page 36 $35 ($55) mcucr sre srw10 se sm1 isc11 isc10 isc01 isc00 page 34 $34 ($54) mcusr - - - - wdrf borf extrf porf page 29 $33 ($53) tccr0 foc0 pwm0 com01 com00 ctc0 cs02 cs01 cs00 page 42 $32 ($52) tcnt0 timer/counter0 counter register page 44 $31 ($51) ocr0 timer/counter0 output compare register page 44 $30 ($50) sfior - - - - - - psr2 psr10 page 39 $2f ($4f) tccr1a com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 page 50 $2e ($4e) tccr1b icnc1 ices1 - - ctc1 cs12 cs11 cs10 page 52 $2d ($4d) tcnt1h timer/counter1 - counter register high byte page 53 $2c ($4c) tcnt1l timer/counter1 - counter register low byte page 53 $2b ($4b) ocr1ah timer/counter1 - output compare register a high byte page 54 $2a ($4a) ocr1al timer/counter1 - output compare register a low byte page 54 $29 ($49) ocr1bh timer/counter1 - output compare register b high byte page 54 $28 ($48) ocr1bl timer/counter1 - output compare register b low byte page 54 $27 ($47) tccr2 foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 page 42 $26 ($46) assr - - - - as20 tcon2ub ocr2ub tcr2ub page 47 $25 ($45) icr1h timer/counter1 - input capture register high byte page 54 $24 ($44) icr1l timer/counter1 - input capture register low byte page 54 $23 ($43) tcnt2 timer/counter2 counter register page 44 $22 ($42) ocr2 timer/counter2 output compare register page 44 $21 ($41) wdtcr - - - wdtoe wde wdp2 wdp1 wdp0 page 58 $20 ($40) ubrrhi ubrr1[11:8] ubrr0[11:8] page 78 $1f ($3f) eearh - - - - - - - eear8 page 60 $1e ($3e) eearl eeprom address register low byte page 60 $1d ($3d) eedr eeprom data register page 60 $1c ($3c) eecr - - - - eerie eemwe eewe eere page 61 $1b ($3b) porta porta7 porta6 porta5 port a4 porta3 porta2 porta1 porta0 page 89 $1a ($3a) ddra dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 page 89 $19 ($39) pina pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 page 89 $18 ($38) portb portb7 portb6 portb5 port b4 portb3 portb2 portb1 portb0 page 91 $17 ($37) ddrb ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 page 91 $16 ($36) pinb pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 page 91 $15 ($35) portc portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 page 98 $14 ($34) ddrc ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 page 98 $13 ($33) pinc pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 page 98 $12 ($32) portd portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 page 100 $11 ($31) ddrd ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 page 100 $10 ($30) pind pind7 pind6 pind5 pind4 pind3 pind2 pind1 pind0 page 100 $0f ($2f) spdr spi data register page 68 $0e ($2e) spsr spif wcol - - - - - spi2x page 67 $0d ($2d) spcr spie spe dord mstr cpol cpha spr1 spr0 page 66 $0c ($2c) udr0 uart0 i/o data register page 73 $0b ($2b) ucsr0a rxc0 txc0 udre0 fe0 or0 - u2x0 mpcm0 page 73 $0a ($2a) ucsr0b rxcie0 txcie0 udrie0 rxen0 txen0 chr90 rxb80 txb80 page 75 $09 ($29) ubrr0 uart0 baud rate register page 78 $08 ($28) acsr acd ainbg aco aci acie acic acis1 acis0 page 81 $07 ($27) porte - - - - - porte2 porte1 porte0 page 106 $06 ($26) ddre - - - - - dde2 dde1 dde0 page 106 $05 ($25) pine - - - - - pine2 pine1 pine0 page 106 $04 ($24) reserved $03 ($23) udr1 uart1 i/o data register page 73 $02 ($22) ucsr1a rxc1 txc1 udre1 fe1 or1 - u2x1 mpcm1 page 75 $01 ($21) ucsr1b rxcie1 txcie1 udrie1 rxen1 txen1 chr91 rxb81 txb81 page 73 $00 ($20) ubrr1 uart1 baud rate register page 78
145 atmega161(l) 1228d?avr?02/07 notes: 1. for compatibility with future devices, reserved bits shoul d be written to zero if accessed. reserved i/o memory address es should never be written. 2. some of the status flags are cleared by writing a logical ?1? to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instruction s work with registers $00 to $1f only.
146 atmega161(l) 1228d?avr?02/07 instruction set summary mnemonic operands description operation flags # clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl, k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl, k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd $ff - rd z,c,n,v 1 neg rd two?s complement rd $00 - rd z,c,n,v,h 1 sbr rd, k set bit(s) in register rd rd v k z,n,v 1 cbr rd, k clear bit(s) in register rd rd ? ($ff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd - 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd $ff none 1 mul rd, rr multiply unsigned r1:r0 rd x rr z,c 2 muls rd, rr multiply signed r1:r0 rd x rr z,c 2 mulsu rd, rr multiply signed with unsigned r1:r0 rd x rr z,c 2 fmul rd, rr fractional multiply unsigned r1:r0 (rd x rr) << 1 z,c 2 fmuls rd, rr fractional multiply signed r1:r0 (rd x rr) << 1 z,c 2 fmulsu rd, rr fractional multiply signed with unsigned r1:r0 (rd x rr) << 1 z,c 2 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc znone2 jmp k direct jump pc knone3 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 call k direct subroutine call pc knone4 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd, rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1/2/3 cp rd, rr compare rd - rr z,n,v,c,h 1 cpc rd, rr compare with carry rd - rr - c z,n,v,c,h 1 cpi rd, k compare register with immediate rd - k z,n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b) = 0) pc pc + 2 or 3 none 1/2/3 sbrs rr, b skip if bit in register is set if (rr(b) = 1) pc pc + 2 or 3 none 1/2/3 sbic p, b skip if bit in i/o register cleared if (p(b) = 0) pc pc + 2 or 3 none 1/2/3 sbis p, b skip if bit in i/o register is set if (p(b) = 1) pc pc + 2 or 3 none 1/2/3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc + k + 1 none 1/2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc + k + 1 none 1/2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1/2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1/2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1/2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1/2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1/2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1/2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1/2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1/2 brge k branch if greater or equal, signed if (n v = 0) then pc pc + k + 1 none 1/2 brlt k branch if less than zero, signed if (n v = 1) then pc pc + k + 1 none 1/2 brhs k branch if half-carry flag set if (h = 1) then pc pc + k + 1 none 1/2 brhc k branch if half-carry flag cleared if (h = 0) then pc pc + k + 1 none 1/2
147 atmega161(l) 1228d?avr?02/07 brts k branch if t-flag set if (t = 1) then pc pc + k + 1 none 1/2 brtc k branch if t-flag cleared if (t = 0) then pc pc + k + 1 none 1/2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1/2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1/2 brie k branch if interrupt enabled if (i = 1) then pc pc + k + 1 none 1/2 brid k branch if interrupt disabled if (i = 0) then pc pc + k + 1 none 1/2 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, -x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, -y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd, y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z + 1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st -x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st -y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q, rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q, rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc. rd (z), z z + 1 none 3 spm store program memory (z) r1:r0 none - in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 bit and bit-test instructions sbi p, b set bit in i/o register i/o(p,b) 1none2 cbi p, b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c, rd(n+1) rd(n), c rd(7) z,c,n,v 1 ror rd rotate right through carry rd(7) c, rd(n) rd(n+1), c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n = 0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4), rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1 sreg(s) 1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0c1 sen set negative flag n 1n1 cln clear negative flag n 0n1 instruction set summary (continued) mnemonic operands description operation flags # clocks
148 atmega161(l) 1228d?avr?02/07 sez set zero flag z 1z1 clz clear ze ro flag z 0z1 sei global interrupt enable i 1i1 cli global interrupt disable i 0i1 ses set signed test flag s 1s1 cls clear signed test flag s 0s1 sev set two?s complement overflow v 1v1 clv clear two?s complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half-carry flag in sreg h 1h1 clh clear half-carry flag in sreg h 0 h 1 nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 3 wdr watchdog reset (see specific descr. for wdr/timer) none 1 instruction set summary (continued) mnemonic operands description operation flags # clocks
149 atmega161(l) 1228d?avr?02/07 note: this device can also be supplied in wafer form. please contac t your local atmel sales office for detailed ordering informa tion and minimum quantities. ordering information speed (mhz) power supply ordering code package operation range 4 2.7 - 5.5v atmega161-4ac atmega161-4pc 44a 40p6 commercial (0 c to 70 c) atmega161-4ai atmega161-4pi 44a 40p6 industrial (-40 c to 85 c) 8 4.0 - 5.5v atmega161-8ac atmega161-8pc 44a 40p6 commercial (0 c to 70 c) atmega161-8ai atmega161-8pi 44a 40p6 industrial (-40 c to 85 c) package type 44a 44-lead, thin (1.0 mm) plastic quad flat package (tqfp) 40p6 40-lead, 0.600" wide, plastic dual inline package (pdip)
150 atmega161(l) 1228d?avr?02/07 packaging information 44a 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 44a, 44-lead, 10 x 10 mm body size, 1.0 mm body thickness, 0.8 mm lead pitch, thin profile plastic quad flat package (tqfp) b 44a 10/5/2001 pin 1 identifier 0?~7? pin 1 l c a1 a2 a d1 d e e1 e b common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this package conforms to jedec reference ms-026, variation acb. 2. dimensions d1 and e1 do not include mold protrusion. allowable protrusion is 0.25 mm per side. dimensions d1 and e1 are maximum plastic body size dimensions including mold mismatch. 3. lead coplanarity is 0.10 mm maximum. a ? ? 1.20 a1 0.05 ? 0.15 a2 0.95 1.00 1.05 d 11.75 12.00 12.25 d1 9.90 10.00 10.10 note 2 e 11.75 12.00 12.25 e1 9.90 10.00 10.10 note 2 b 0.30 ? 0.45 c 0.09 ? 0.20 l 0.45 ? 0.75 e 0.80 typ
151 atmega161(l) 1228d?avr?02/07 40p6 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 44a, 44-lead, 10 x 10 mm body size, 1.0 mm body thickness, 0.8 mm lead pitch, thin profile plastic quad flat package (tqfp) b 44a 10/5/2001 pin 1 identifier 0?~7? pin 1 l c a1 a2 a d1 d e e1 e b common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this package conforms to jedec reference ms-026, variation acb. 2. dimensions d1 and e1 do not include mold protrusion. allowable protrusion is 0.25 mm per side. dimensions d1 and e1 are maximum plastic body size dimensions including mold mismatch. 3. lead coplanarity is 0.10 mm maximum. a ? ? 1.20 a1 0.05 ? 0.15 a2 0.95 1.00 1.05 d 11.75 12.00 12.25 d1 9.90 10.00 10.10 note 2 e 11.75 12.00 12.25 e1 9.90 10.00 10.10 note 2 b 0.30 ? 0.45 c 0.09 ? 0.20 l 0.45 ? 0.75 e 0.80 typ
152 atmega161(l) 1228d?avr?02/07 errata atmega161 rev. e  pwm not phase correct  increased interrupt latency  interrupt return fails when stack pointer addresses the external memory  writing ubbrh affects both uart0 and uart1  store program memory instruction may fail 5. pwm not phase correct in phase correct pwm mode, a change from ocrx = top to anything less than top does not change the ocx output. this gives a phase error in the following period. problem fix/workaround make sure this issue is no t harmful to the application. 4. increased interrupt latency in this device, some instructions are not interruptable, and will cause the interrupt latency to increase. the only practical problem concerns a loop followed by a two- word instruction while waiting for an interrupt. the loop may consist of a branch instruction or an absolute or relati ve jump back to itself like this: loop: rjmp loop in this case, a dead-lock situation arises. problem fix/workaround in assembly, insert a nop instruction immediately after a loop to itself. the problem will normally be detected du ring development. in c, th e only construct that will give this problem is an empty ?for? loop; ?for(;;)?. use ?while(1)? or ?do{} while (1)? to avoid the problem. 3. interrupt return fails when stack pointer addresses the external memory when stack pointer addresses external memory (sph:spl > $45f), returning from interrupt will fail. the program counter will be updated with a wrong value and thus the program flow will be corrupted. problem fix/workaround address the stack pointer to internal sram or disable interrupts while stack pointer addresses external memory. 2. writing ubbrh affects both uart0 and uart1 writing ubrrhi updates baud rate generator for both uart0 and uart1. the baud rate generator's counter is updated each time either ubrr or ubrrhi are written. since the ubrrhi register is shared by uart0 and uart1, changing the baud rate for one uart will affect the operation of the other uart. problem fix/workaround do not update ubrrhi for one uart when transmitting/receiving data on the other.
153 atmega161(l) 1228d?avr?02/07 1. store program memory instruction may fail at certain frequencies and voltages, the store program memory (spm) instruction may fail. problem fix/workaround avoid using the spm instruction.
154 atmega161(l) 1228d?avr?02/07 data sheet change log for atmega161 this document contains a log on the changes made to the data sheet for atmega161. changes from rev. 1228c-08/02 to rev. 1228d-02/07 1 package drawing updated from rev. a to rev. b. 2 written ?not recommend in new designs? on features page. changes from rev. 1228b-09/01 to rev. 1228c-08/02 all page numbers refers to this document. 1 description of brown-out detector (bod) removed from data sheet.
i atmega161(l) 1228d?avr?02/07 table of contents features............... ................. .............. .............. .............. .............. ......... 1 disclaimer............ ................. .............. .............. .............. .............. ......... 1 pin configuration....... ................. ................ ................. ................ ......... 2 description .......... ................. .............. .............. .............. .............. ......... 3 block diagram ...................................................................................................... 4 pin descriptions.................................................................................................... 5 crystal oscillator................................................................................................... 6 architectural overview........ .............. .............. .............. .............. ......... 7 the general purpose register file .................................................................... 10 alu ? arithmetic logic unit................................................................................ 11 self-programmable flash program memory....................................................... 11 eeprom data memory............. ................ ................ ................ ................ ......... 11 sram data memory........................................................................................... 12 program and data addressing modes................................................................ 13 memory access times and instruction execution timing .................................. 17 l/o memory ......................................................................................................... 19 reset and interrupt handling.............................................................................. 22 mcu control register ? mcucr ....................................................................... 34 sleep modes....................................................................................................... 36 timer/counters .......... ................. ................ ................. .............. ......... 38 timer/counter prescalers................................................................................... 38 8-bit timer/counters t/c0 and t/c2 .................................................................. 40 timer/counter1................................................................................................... 49 watchdog timer............ ................ ................. .............. .............. ......... 58 eeprom read/write access..... ................ ................. .............. ......... 60 prevent eeprom corruption ............................................................................. 62 serial peripheral interface ? spi................... .............. .............. ......... 63 ss pin functionality............................................................................................ 65 data modes ........................................................................................................ 65 uarts................ ................ .............. ............... .............. .............. ......... 69 data transmission.............................................................................................. 69 data reception ................................................................................................... 71 uart control ..................................................................................................... 73 baud rate generator.......................................................................................... 76 double-speed transmission ............................................................................... 78 analog comparator ............... ................ ................. ................ ............ 81
ii atmega161(l) 1228d?avr?02/07 internal voltage reference ... ................ ................. ................ ............ 83 voltage reference enable signals and start-up time ....................................... 83 interface to external memory ............... ................. ................ ............ 84 using the external memory interface ................................................................. 88 i/o ports............. ................ .............. ............... .............. .............. ......... 89 port a.................................................................................................................. 89 port b.................................................................................................................. 91 port b schematics .............................................................................................. 94 port c.................................................................................................................. 98 port d................................................................................................................ 100 port e................................................................................................................ 106 memory programming........... ................ ................. ................ .......... 110 boot loader support......................................................................................... 110 entering the boot loader program ................................................................... 111 capabilities of the boot loader......................................................................... 112 self-programming the flash ............................................................................. 112 program memory lock bits .... ................. ................ .............. .......... 116 fuse bits ........................................................................................................... 116 signature bytes ................................................................................................ 117 programming the flash and eeprom......... ................ ................ ............. ....... 117 parallel programming ....................................................................................... 118 parallel programming characteristics .............................................................. 124 serial downloading........................................................................................... 125 serial programming characteristics ................................................................. 129 electrical characteristics...... ................ ................. ................ .......... 130 absolute maximum ratings*............................................................................. 130 dc characteristics............ .............. ............... .............. .............. ....... 131 external clock drive waveforms ...................................................................... 132 external data memory timing .......................................................................... 133 typical characteristics ......... ................ ................. ................ .......... 138 register summary ..... ................. ................ .............. .............. .......... 144 instruction set summary ...... ................ ................. ................ .......... 146 ordering information........... ................ ................. ................ ............ 149 packaging information .......... ................ ................. ................ .......... 150 44a ................................................................................................................... 150
iii atmega161(l) 1228d?avr?02/07 40p6 ................................................................................................................. 151 errata ............... ................ .............. .............. .............. .............. .......... 152 atmega161 rev. e .......................................................................................... 152 data sheet change log fo r atmega161 ......... ............ ............ ....... 154 changes from rev. 1228b-09/01 to rev. 1228c-08/02 ................................... 154 table of contents ................ .............. .............. .............. .............. .......... i
iv atmega161(l) 1228d?avr?02/07
1228d?avr?02/07 ? 2007 atmel corporation . all rights reserved. at m e l ? , logo and combinations thereof, everywhere you are ? , avr ? , avr studio ? , and oth- ers, are registered trademarks or trademarks of atmel corporati on or its subsidiaries. other terms and product names may be tra demarks of oth- ers. disclaimer: the information in this document is provided in connection with atmel products. no license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of atmel products. except as set forth in atmel?s terms and condi- tions of sale located on atmel? s web site, atmel assumes no liability whatsoever and disclaims any express, implied or statutor y warranty relating to its products including, but not limited to , the implied warranty of merchantability, fitness for a particu lar purpose, or non-infringement. in no event shall atmel be liable for any direct, indirect, conseque ntial, punitive, special or i nciden- tal damages (including, without limitation, damages for loss of profits, business interruption, or loss of information) arising out of the use or inability to use this document, even if at mel has been advised of the possibility of such damages. atmel makes no representations or warranties with respect to the accuracy or co mpleteness of the contents of this document and reserves the rig ht to make changes to specifications and product descriptions at any time without notice. atmel does not make any commitment to update the information contained her ein. unless specifically provided otherwise, atmel products are not suitable for, and shall not be used in, automotive applications. atmel?s products are not int ended, authorized, or warranted for use as components in applications in tended to support or sustain life. atmel corporation atmel operations 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 487-2600 regional headquarters europe atmel sarl route des arsenaux 41 case postale 80 ch-1705 fribourg switzerland tel: (41) 26-426-5555 fax: (41) 26-426-5500 asia room 1219 chinachem golden plaza 77 mody road tsimshatsui east kowloon hong kong tel: (852) 2721-9778 fax: (852) 2722-1369 japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel: (81) 3-3523-3551 fax: (81) 3-3523-7581 memory 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 microcontrollers 2325 orchard parkway san jose, ca 95131, usa tel: 1(408) 441-0311 fax: 1(408) 436-4314 la chantrerie bp 70602 44306 nantes cedex 3, france tel: (33) 2-40-18-18-18 fax: (33) 2-40-18-19-60 asic/assp/smart cards zone industrielle 13106 rousset cedex, france tel: (33) 4-42-53-60-00 fax: (33) 4-42-53-60-01 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 scottish enterprise technology park maxwell building east kilbride g75 0qr, scotland tel: (44) 1355-803-000 fax: (44) 1355-242-743 rf/automotive theresienstrasse 2 postfach 3535 74025 heilbronn, germany tel: (49) 71-31-67-0 fax: (49) 71-31-67-2340 1150 east cheyenne mtn. blvd. colorado springs, co 80906, usa tel: 1(719) 576-3300 fax: 1(719) 540-1759 biometrics/imagin g/hi-rel mpu/ high speed converters/rf datacom avenue de rochepleine bp 123 38521 saint-egreve cedex, france tel: (33) 4-76-58-30-00 fax: (33) 4-76-58-34-80 literature requests www.atmel.com/literature at m e l ? and avr ? are the registered trademarks of atmel. other terms and product names may be the trademarks of others.


▲Up To Search▲   

 
Price & Availability of ATMEGA16107

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X